Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a component class needed to be reopened to specify positional params?

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
like image 558
akst Avatar asked Nov 24 '15 01:11

akst


People also ask

Why do we annotate components in a schematic?

The Annotate Schematics command systematically assigns designators to all or selected parts in selected sheets of a project and ensures that designators are unique and ordered based on their position.

Which methods are called when the state or props of a component is changed?

An update can be caused by changes to props or state. These methods are called in the following order when a component is being re-rendered: static getDerivedStateFromProps() shouldComponentUpdate() render()

What interface must a class implement in order to be a listener for Button Actionevents *?

The DemoButtonHandler class implements the ActionListener interface, which requires an actionPerformed method with an ActionEvent parameter. The constructor stores the button that the event handler is listening to (this can also be accessed by event.

What are the GUI components in Java?

GUI Component classes, such as Button , TextField , and Label . GUI Container classes, such as Frame and Panel . Layout managers, such as FlowLayout , BorderLayout and GridLayout . Custom graphics classes, such as Graphics , Color and Font .


1 Answers

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.

like image 88
spectras Avatar answered Sep 28 '22 19:09

spectras