最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember - How to addremove elements from an array property on controller - Stack Overflow

programmeradmin4浏览0评论

Given a controller in ember:

export default Ember.Controller.extend({
  stringProp: "",
  arrayProp: []
});

You can, for example, set the string property with this.set('stringProp', "Blah blah"). But that is overriding. What I want to do is push to the array property.

Is there a better (either shorter or faster) way than this:

this.set('arrayProp', this.get('arrayProp').push(element));

Also, is there a shortcut for removing elements from such an array property?

Given a controller in ember:

export default Ember.Controller.extend({
  stringProp: "",
  arrayProp: []
});

You can, for example, set the string property with this.set('stringProp', "Blah blah"). But that is overriding. What I want to do is push to the array property.

Is there a better (either shorter or faster) way than this:

this.set('arrayProp', this.get('arrayProp').push(element));

Also, is there a shortcut for removing elements from such an array property?

Share Improve this question edited Aug 6, 2015 at 12:14 user663031 asked Aug 6, 2015 at 9:54 Marco PrinsMarco Prins 7,41913 gold badges47 silver badges79 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

You are looking for pushObject, removeObject, etc. See http://emberjs.com/api/classes/Ember.MutableArray.html.

this.get('arrayProp').pushObject(element);

For correct behavior by computed properties and observers, it is strongly recommended you use these methods instead of push or other native Array methods.

发布评论

评论列表(0)

  1. 暂无评论