如果我在我的模型中使用ArrayProxy,我找不到更新该数组中特定元素的属性的方法.
这是我的示例路由和控制器:
App.IndexRoute = Ember.Route.extend({
model: function() {
return Ember.ArrayProxy.create({
content : [
{ firstName : "Bob", lastName : "Smith" },
{ firstName : "George", lastName : "Jhnsn"}
]
});
}
});
App.IndexController = Ember.Controller.extend({
actions : {
fixGeorgesLastName : function(){
// how to I change the last name to 'Johnson' ???
console.log(this.get("model").objectAt(1).lastName); // Jhnsn
var georgeObjFromModel = this.get("model").objectAt(1);
console.log(georgeObjFromModel.lastName); // Jhnsn
// georgeObjFromModel.objectAt(1).lastName = "Johnson"; // error !!! TypeError: Object #<Object> has no method 'objectAt'
}
}
});
Run Code Online (Sandbox Code Playgroud)
如何更改lastName第二个元素的值?
我可以弄清楚如何为同一个数组的本地版本做到这一点:
// local instance of same array
var localCustomerArray = Ember.ArrayProxy.create({
content : [
{ firstName : "Bob", lastName : "Smith" },
{ firstName : "George", lastName : "Jhnsn"}
]
});
// works fine
console.log("before: " + localCustomerArray.objectAt(1).lastName); // Jhnsn
localCustomerArray.objectAt(1).lastName = "Johnson";
console.log("after: " + localCustomerArray.objectAt(1).lastName); // Johnson
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
通过这个问题找到答案
它可以使用以下Ember.set方法更新:
var georgeObjFromModel = this.get("model").objectAt(1);
Ember.set(georgeObjFromModel, "lastName", "Johnson");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4201 次 |
| 最近记录: |