5

When specifying a positional param for a component class in ember, you have to reopen the class (like below) so it will work, you cannot include it in the initial declaration (at least from what I've seen of examples and my own experience).

import Ember from 'ember';

const component = Ember.Component.extend({ });

component.reopenClass({
  positionalParams: ['post'],
});

export default component;

If you do it in the single declaration (like below) it will not work

import Ember from 'ember';

export default Ember.Component.extend({
  positionalParams: ['post'],
});

Questions

  • Was this something overlooked in the design process? Or was this deliberate (maybe to discourage usage)?
  • Is this because it's a class attribute of some kind? Is there any way to specify class attributes in the class declaration?

It just feels gross that I can't make it part of the same expression and have to assign the class to a variable, reopen it, then finally export it.


Versions

  • ember.js @ 2.2

1 Answer 1

5

Is this because it's a class attribute of some kind?

This. The reopenClass and extend methods do not do the same thing.

  • Properties passed to reopenClass are put on the constructor of the class itself:

    MyClass = Ember.Object.extend({});
    MyClass.reopenClass({ foo: 42 });
    obj = MyClass.create();
    
    console.log(obj.foo);              // undefined
    console.log(MyClass.foo);          // 42
    console.log(obj.constructor.foo);  // 42
    
  • Properties passed to extend, on the other hand, are available (*) on the instances:

    MyClass = Ember.Object.extend({ foo: 42 });
    obj = MyClass.create();
    
    console.log(obj.foo);              // 42
    console.log(MyClass.foo);          // undefined
    console.log(obj.constructor.foo);  // undefined
    

(*) I don't know whether they are copied or made part of the prototype chain.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.