Javascript 如何在Ember.js中推送/弹出数组?
我可以在余烬对象中包含一个数组,并使用手柄显示内容。但是,我只能使用set()替换数组内容。如何使用push/pop/etc修改数组内容,并且仍然更新UI绑定Javascript 如何在Ember.js中推送/弹出数组?,javascript,sproutcore,ember.js,Javascript,Sproutcore,Ember.js,我可以在余烬对象中包含一个数组,并使用手柄显示内容。但是,我只能使用set()替换数组内容。如何使用push/pop/etc修改数组内容,并且仍然更新UI绑定 // JS App.obj = Ember.Object.create({ "things": ["1", "2"], }); App.obj.set("things", ["1", "2", "3"]); // Works App.obj.things.push("3"); // Doesn't Work // HTML +
// JS
App.obj = Ember.Object.create({
"things": ["1", "2"],
});
App.obj.set("things", ["1", "2", "3"]); // Works
App.obj.things.push("3"); // Doesn't Work
// HTML + Handlebars
{{#with App.obj}}
<ul>
{{#each things}}
<li>{{this}}</li>
{{/each}}
</ul>
{{/with}}
//JS
App.obj=Ember.Object.create({
“事物”:[“1”、“2”],
});
App.obj.set(“things”、[“1”、“2”、“3”);//作品
App.obj.things.push(“3”);//不起作用
//HTML+把手
{{{带App.obj}
{{{每件事}
- {{this}}
{{/每个}}
{{/与}}
为了使用集合,Ember.js提供了一个数组包装类Ember.Array/Ember.MutableArray
因此,与其使用普通数组,不如使用以下内容:
// JS
App.obj = Ember.Object.create({
"things": Ember.A(["1", "2"])
});
App.obj.things.pushObject("3"); // pushObject notifies observers
// HTML + Handlebars
{{#with App.obj}}
<ul>
{{#each things}}
<li>{{this}}</li>
{{/each}}
</ul>
{{/with}}
//JS
App.obj=Ember.Object.create({
“事物”:余烬A([“1”,“2”])
});
App.obj.things.pushObject(“3”);//pushObject通知观察者
//HTML+把手
{{{带App.obj}
{{{每件事}
- {{this}}
{{/每个}}
{{/与}}
使用Ember.ArrayController实例,只需使用[]声明一个数组,也将创建Ember.ArrayController类的数组
如果要在Ember ArrayController的末尾添加对象,可以使用addObject()方法
例如
pushObject和it好友(popObject、removeAt等)的有用参考资料:这里还有一个有用的答案,说明了为什么需要使用这些方法:最好现在避免使用Ember.ArrayController,因为它已被弃用。改用Ember.Array或Ember.ArrayProxy类。
mutableArray:[],
setModel:function(){
var data1={'id':1,'name':'over'};
var data2={'id':3,'name':'out'};
this.get('mutableArray').addObject(data1);
this.get('mutableArray').addObject(data2);
/* To Add Object to middle of array at given index simply use the native array splice method */
var data1={'id':2,'name':'and'}
this.get('mutableArray').splice(1,0,data1);
return this.get('mutableArray')
}.property('mutableArray')