How to push/pop arrays in Ember.js?

前端 未结 2 715
误落风尘
误落风尘 2021-02-01 14:39

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 cont

2条回答
  •  旧巷少年郎
    2021-02-01 15:12

    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')
    

提交回复
热议问题