我只是潛入Ember。我正在尋找一種方法來傳遞一個普通的香草對象數組到一個集合/控制器,並讓他們輸入到正確的模型。將香草物體自動澆鑄到灰燼物體上
這裏的簡單集合視圖:
{{#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
method--它沒有被正確地投給我Vix.Prod
灰燼對象。
使用這種替代:
Vix.prodsController.pushObjects(prods);
產生相同的結果。這只是如果我明確地創建,我得到了get/set
善新模型實例:
var prods = [Vix.Prod.create({id:"yermom1", title:"yermom 1"}), {Vix.Prod.create(id:"yermom2", title:"yermom 2"})]
是否有自動類型轉換的香草對象我Vix.Prod灰燼對象的方法嗎?如果不是,我是唯一真正想要類似的東西嗎?在Backbone中,可以在集合上設置model
屬性。我想我可以在我的控制器上創建一個setter來做類似的事情,只是想知道是否有內置的東西我不見了。謝謝!
這似乎不工作了。當我嘗試這個時,我得到'你不應該在模型上調用'創建'。相反,用你想設置的屬性調用'store.createRecord' – 2014-10-23 04:05:41