How to push/pop arrays in Ember.js?

♀尐吖头ヾ 提交于 2019-12-02 18:49:50

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}}

Use an instance of Ember.ArrayController,simply declaring an array with [] will also create array of Ember.ArrayController class.

If you want to add an Object at the end of Ember ArrayController you can use the addObject() method;

eg.

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')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!