Ember.js 将香草对象自动类型转换为余烬对象

Ember.js 将香草对象自动类型转换为余烬对象,ember.js,Ember.js,我正潜入灰烬。我正在寻找一种方法,将普通对象的普通数组传递到集合/控制器中,并将它们类型转换为正确的模型 以下是简单的集合视图: {{#collection id="prods" contentBinding="Vix.prodsController" tagName="ul"}} {{content.title}} {{/collection}} 模型如下: Vix.Prod = Ember.Object.extend({ id: null, title: null }); 以

我正潜入灰烬。我正在寻找一种方法,将普通对象的普通数组传递到集合/控制器中,并将它们类型转换为正确的模型

以下是简单的集合视图:

{{#collection id="prods" contentBinding="Vix.prodsController" tagName="ul"}}
  {{content.title}}
{{/collection}}
模型如下:

Vix.Prod = Ember.Object.extend({
  id: null,
  title: null
});
以及控制器:

Vix.prodsController = Ember.ArrayController.create({
  content: []
});
然后让我们从服务器获取一些JSON格式的数据。在本例中,我将对其进行硬编码:

var prods = [{id:"yermom1", title:"yermom 1"}, {id:"yermom2", title:"yermom 2"}]

Vix.prodsController.set('content', prods);
到目前为止还不错。我得到了我的
li
元素的简单列表,显示了我所期望的标题。但当我要更新其中一个对象的标题时,请使用:

Vix.prodsController.objectAt(0).set('title', 'new title')
它抱怨是因为对象没有
set
方法——它没有正确地转换到我的
Vix.Prod
Ember对象

使用此替代方案:

Vix.prodsController.pushObjects(prods);
产生相同的结果。只有当我显式地创建新的模型实例时,我才能得到
get/set
goodness:

var prods = [Vix.Prod.create({id:"yermom1", title:"yermom 1"}), {Vix.Prod.create(id:"yermom2", title:"yermom 2"})]

有没有办法自动将这些普通对象类型转换为我的Vix.Prod-Ember对象?如果不是,我是唯一一个真正想要这样的东西的人吗?在主干网中,可以对集合设置
model
属性。我想我可以在我的控制器上创建一个setter来做类似的事情——只是想知道我是否缺少一些内置的东西。谢谢

没有魔法。我建议对模型做一个循环

var prods = [{id:"yermom1", title:"yermom 1"}, {id:"yermom2", title:"yermom 2"}];

for (var i = 0; i < prods.length; i++) {
    prods[i] = Vix.Prod.create(prods[i]); 
}
var prods=[{id:“yermom1”,title:“yermom1”},{id:“yermom2”,title:“yermom2”}];
对于(变量i=0;i
没有魔法。我建议对模型做一个循环

var prods = [{id:"yermom1", title:"yermom 1"}, {id:"yermom2", title:"yermom 2"}];

for (var i = 0; i < prods.length; i++) {
    prods[i] = Vix.Prod.create(prods[i]); 
}
var prods=[{id:“yermom1”,title:“yermom1”},{id:“yermom2”,title:“yermom2”}];
对于(变量i=0;i
如果我尽可能多地使用余烬,我会想要一个快捷方式。这就是我现在所做的。我创建了一个基本集合类,用于创建集合/控制器:

Vix.Collection = Ember.ArrayController.extend({

  model: null, 

  pushObject: function(obj) {
    if (this.get('model') && obj.__proto__.constructor !== this.get('model')) {
      obj = this.get('model').create(obj); 
    }    
    return this._super(obj);
  },

  pushObjects: function(objs) {
    if (this.get('model')) {
      objs = this._typecastArray(objs)
    }
    return this._super(objs);
  },

  set: function(prop, val) {
    if (prop === 'content' && this.get('model')) {
      val = this._typecastArray(val);
    }
    return this._super(prop, val);
  },

  _typecastArray: function(objs) {
    var typecasted = [];
    objs.forEach(function(obj){
      if (obj.__proto__.constructor !== this.get('model')) {
        obj = this.get('model').create(obj);
      } 
      typecasted.push(obj);
    }, this);
    return typecasted;
  }

})

现在,当我调用
pushObject
pushObjects
.set('collection',data)
时,如果集合实例具有已定义的
model
属性,并且添加到集合中的对象尚未属于该类型,则将强制转换它们。到目前为止一直工作得很好,但我欢迎任何反馈。

如果我尽可能多地使用ember,我会想要一个快捷方式。这就是我现在所做的。我创建了一个基本集合类,用于创建集合/控制器:

Vix.Collection = Ember.ArrayController.extend({

  model: null, 

  pushObject: function(obj) {
    if (this.get('model') && obj.__proto__.constructor !== this.get('model')) {
      obj = this.get('model').create(obj); 
    }    
    return this._super(obj);
  },

  pushObjects: function(objs) {
    if (this.get('model')) {
      objs = this._typecastArray(objs)
    }
    return this._super(objs);
  },

  set: function(prop, val) {
    if (prop === 'content' && this.get('model')) {
      val = this._typecastArray(val);
    }
    return this._super(prop, val);
  },

  _typecastArray: function(objs) {
    var typecasted = [];
    objs.forEach(function(obj){
      if (obj.__proto__.constructor !== this.get('model')) {
        obj = this.get('model').create(obj);
      } 
      typecasted.push(obj);
    }, this);
    return typecasted;
  }

})

现在,当我调用
pushObject
pushObjects
.set('collection',data)
时,如果集合实例具有已定义的
model
属性,并且添加到集合中的对象尚未属于该类型,则将强制转换它们。到目前为止,工作情况良好,但我欢迎任何反馈。

您应该看看余烬数据:

它似乎适合你的需要


到今天为止,它还没有投入生产(如自述文件中所述),但由于积极的开发,它正在迅速走向成熟。

您应该看看余烬数据:

它似乎适合你的需要


到今天为止,它还没有投入生产(如自述文件中所述),但由于积极的开发,它正在迅速走向成熟。

这似乎不再有效。当我尝试这个时,我得到
你不应该在模型上调用'create'。相反,使用您想要设置的属性调用“store.createRecord”。
这似乎不再有效。当我尝试这个时,我得到
你不应该在模型上调用'create'。相反,使用您想要设置的属性调用“store.createRecord”。