Ember.js how to load an array of simple ember objects

What is the best way to create an array of ember objects from an array of json object objects?

I can use SetProperties for each individual object as follows:

var ret = Ember.A(); pojos.forEach(function(obj){ var em = Ember.Object.create({}); emCluster.setProperties(obj); ret.push(emCluster); }); 

But is there one way to get the same result?

+7
source share
4 answers

I would use map instead of forEach :

 pojos.map(function(obj){ return Ember.Object.create().setProperties(obj); }); 
+7
source

Yes:

 var ret = pojos.map(function(data) { return Ember.Object.create(data); }); 
+1
source

I use this in my tutorial application to get json from a remote server and parse it into an array of objects.

 App.Model.reopenClass({ allItems: [], find: function(){ $.ajax({ url: 'http://remote_address/models.json', dataType: 'json', context: this, success: function(data){ data.forEach(function(model){ this.allItems.addObject(App.Model.create(model)) <------------------- }, this) } }) return this.allItems; }, }); 
0
source
 ret = (Em.Object.create pojo for pojo in pojos) 
0
source

All Articles