I’m having a hard time understanding how the base EmberObject
class is supposed to be used and inherited… I could not find the relevant info in the doc…
I want to define a class C
deriving from EmberObject
, and I would like it to have this kind of behavior:
// create new instance and initialize it with data, which is not directly an instance property
let instance = C.create(data);
or maybe let instance = C.createFromData(data)
if the create
method is reserved.
Looking at how this should be done, I looked at the code in ember-file-upload
here and saw something like:
export default EmberObject.extend({
init() {
this._super();
// instance initialization code goes here
},
prop1: null,
prop2: computed(
// some code
),
// etc.
// other instance methods and properties
}).reopenClass({
// that is a static method
createInstance(data) {
let instance = this.create();
Object.defineProperty(instance, 'someMagicProp', {
writeable: false,
enumerable: false,
value: // something which uses 'data'
});
return instance;
}
});
My reaction to this code was “ Why did I go away from Python!!!”
For instance, someMagicProp
is a very important property and is not even in the main class definition block…
So the questions are:
- how can we modify the
create
andinit
methods? - can they have arguments (at least one) which are not directly class properties?
- do we need to do things like
Object.defineProperty
like in the example above, or is it just a way to make this special property protected (writeable: false
) for that particular use case? - bonus question: based on this example, I tried to define a creator function either in the main class definition block, or in the reopen block, and the behavior seems to be different… Is this supposed to be so?