Using underscore.js groupBy with Ember.js -


is possible use underscore's groupby function ember.js?

i have following attempt not working:

var activities = app.store.findmany(app.activity, feed.mapproperty('id').uniq())  var grouped = _.groupby(activities, function(activity){   return activity.get('datelabel;') }); 

i following error:

object app.activity has no method 'get'

the store loaded correct data findmany not make remote call.

the problem findmany returns ds.manyarray lot different _.groupby looking for.

you implement own groupby function tailored ember-data ds-manyarray objects , extend _ it:

_.emberarraygroupby = function(emberarray, val) {   var result = {}, key, value, i, l = emberarray.get('length'),       iterator = _.isfunction(val) ? val : function(obj) { return obj.get(val); };    (i = 0; < l; i++) {     value = emberarray.objectat(i);     key   = iterator(value, i);     (result[key] || (result[key] = [])).push(value);   }   return result; }; 

now can call

var grouped = _.emberarraygroupby(activities, function(activity) {   return activity.get('datelabel'); }); 

or more simply

var grouped = _.emberarraygroupby(activities, 'datelabel'); 

the function above based on underscore's original groupby() implementation, looks similar:

_.groupby = function(obj, val) {   var result = {};   var iterator = _.isfunction(val) ? val : function(obj) { return obj[val]; };   each(obj, function(value, index) {     var key = iterator(value, index);     (result[key] || (result[key] = [])).push(value);   });   return result; }; 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -