7

json オブジェクト オブジェクトの配列から ember オブジェクトの配列を作成する最良の方法は何ですか?

次のように、個々のオブジェクトごとに SetProperties を使用できます。

var ret = Ember.A();

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

しかし、同じ結果を得る1行の方法はありますか?

4

4 に答える 4

7

私はmap代わりに使用したいforEach

pojos.map(function(obj){
  return Ember.Object.create().setProperties(obj);
});
于 2012-06-07T17:00:37.647 に答える
1

はい:

var ret = pojos.map(function(data) { return Ember.Object.create(data); });
于 2012-06-07T15:40:43.107 に答える
0

これをトレーニング アプリで使用して、リモート サーバーから json を取得し、それを解析してオブジェクトの配列にします。

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;
  },
});
于 2013-02-11T09:41:23.367 に答える
0
ret = (Em.Object.create pojo for pojo in pojos)
于 2014-05-28T19:59:57.490 に答える