6

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.