如何在Ember.js中推/ pop数组? [英] How to push/pop arrays in Ember.js?
本文介绍了如何在Ember.js中推/ pop数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在Ember对象中包含一个数组,并使用Handlebars显示内容。但是,我只能使用set()替换数组内容。如何使用push / pop / etc修改数组内容。还有UI绑定更新?
I can include an array in an Ember object, and display the contents using Handlebars. However, I can only replace the array contents using set(). How can I modify the array contents using push/pop/etc. and still have the UI bindings update?
// 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}}
推荐答案
使用集合,Ember.js提供了一个数组包装器类Ember.Array / Ember.MutableArray
For working with collections, Ember.js provides an Array wrapper class, Ember.Array / Ember.MutableArray
所以,而不是使用一个纯数组,使用这些:
So, instead of using a plain array, use these:
// 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}}
这篇关于如何在Ember.js中推/ pop数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文