ember.js version: 2.18.0
model:
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
songs: DS.hasMany('song')
})
controller:
export default Controller.extend({
sortBy: ['name:asc', 'description:asc'],
sortedBands: computed.sort('model', 'sortBy')
}
ember gave me the following error:
Only string, number, symbol, boolean, null, undefined, and function are allowed as default properties ember/avoid-leaking-state-in-ember-objects
I had the same issue yesterday and found this blog post:
https://dockyard.com/blog/2015/09/18/ember-best-practices-avoid-leaking-state-into-factories
tl;dr
Donât use objects (arrays) as default properties on Ember.Objects because they would be shared by all instances of this Ember.Object.
You should use a computed property
sortBy: computed(function() {
return ['name:asc', 'description:asc'];
})
or set the property in the init hook
init() {
this._super(...arguments);
this.sortBy = ['name:asc', 'description:asc'];
}
1 Like
An object class variable defaults to a static variable really is a quirk.
Thanks anyway.
l_tonz
January 12, 2018, 4:31am
4
I think the lesson learned is that try to use the Object interface methods as much as possible instead of the JavaScript primitives. If that makes sense.
For example: this.get(ââ), this.set(ââ,ââ), computed properties, init(){ this._super(âŚarguments). I think youâd run into less issues. Use Emberâs Object Model until they migrate off it
Another option rather than adding an init
function is to freeze
the array, as I did here :
filteredReimbursementsSorting: Object.freeze(['ride.start']),
This follows the example of Ember superstar Robert Jackson, it seems less intrusive to me.
1 Like
@backspace Thanks for the link. I like âyourâ solution much better.
Youâre welcome. I like it much more too, the init
thing looks so invasive, this feels like it keeps it more declarative rather than imperative.
@backspace Used Object.freeze
with Ember 2.18 but get this error in IE 11.
https://github.com/emberjs/ember.js/issues/16201
broerse
February 10, 2018, 11:10am
9
Just tested and this bug is still present in 3.0.0-beta.6