Code Monkey home page Code Monkey logo

Comments (10)

buschtoens avatar buschtoens commented on May 22, 2024 2

Just noting this down for fellow sufferers. There are a few class attributes like tagName or classNameBindings that need to be specified before the base class's init() method is called.

There are two ways that I know of. I currently prefer the first one because of the slightly less verbose syntax:

export default class extends Component.extend({
  styles,
  tagName: 'span',
  localClassNameBindings: ['iconTrueColored', 'iconFalseColored']
}) {
 layout = layout;

 // ...
}
export default class extends Component {
  layout = layout;

  init() {
    // set class attributes first
    this.tagName = 'span';
    this.styles = styles;
    this.localClassNameBindings = ['iconTrueColored', 'iconFalseColored'];

    // then call base class's init
    super.init(...arguments);

    // other init stuff goes here
  }

  // ...
}

from ember-decorators.

dfreeman avatar dfreeman commented on May 22, 2024 1

@pzuraq isn't it a hard error to access this in a constructor before calling super()?

from ember-decorators.

buschtoens avatar buschtoens commented on May 22, 2024

The first problem is explained by the fact that constructor is called after init and didReceiveAttrs.

from ember-decorators.

buschtoens avatar buschtoens commented on May 22, 2024

Correction: constructor is called before init, however the constructor of Ember.CoreObject instantly calls init etc, before returning to the derived constructor.

export default class extends Component {
  foo = 'bar';

  constructor() {
    console.log('constructor (pre super)');
    super();
    console.log('constructor (post super)', this.foo);
  }

  init() {
    super.init(...arguments);
    console.log('init', this.foo);
  }

  didReceiveAttrs()  {
    this._super(...arguments);
    console.log('didReceiveAttrs', this.foo);
  }
}
constructor (pre super)
init qux
didReceiveAttrs qux
constructor (post super) bar

The transformed Babel output makes clear why foo has a different value:

    function _default() {
      _classCallCheck(this, _default);

      // code before `super();`
      console.log('constructor (pre super)');

      // base constructor gets called here
      var _this = _possibleConstructorReturn(this, (_default.__proto__ || Object.getPrototypeOf(_default)).call(this));

      // default values are set
      _this.foo = 'bar';

      // code after `super();`
      console.log('constructor (post super)', _this.foo);
      return _this;
    }

So that's an explanation for the first problem. Second problem still unsolved.

Maybe this issue should be moved to Ember.js itself?

from ember-decorators.

pzuraq avatar pzuraq commented on May 22, 2024

We've been discussing and putting together an RFC for the behavior of Ember.Object's constructor, which was not public API up until this point. So far it looks like it does need to be refactored, but class properties still won't be overwritten by values passed into create. Instead, you'll have to intercept them in the constructor:

export default class extends Component {
  constructor(props) {
    props.foo = props.foo === undefined ? 'bar' : foo;

    super(props);
  }
}

This is a little inconvenient and counterintuitive compared to today's behavior, but it makes much more sense if/when components start behaving more like Glimmer components where all args are passed in to the component on args:

export default class extends Component {
  foo = 'bar'

  constructor(props) {
    super(props);

    console.log(this.foo); // 'bar';
    console.log(this.args.foo); // 'qux';
  }
}

This is what sold me on keeping the behavior this way, it actually gets rid of the merge behavior of old components and creates a clear separation between args that are passed in and properties that are defined on the class, one that cannot be overwritten. However, I do think the new behavior will be unintuitive to current Ember users.

from ember-decorators.

buschtoens avatar buschtoens commented on May 22, 2024

@dfreeman yup, but these two lines can be swapped, I think

from ember-decorators.

pzuraq avatar pzuraq commented on May 22, 2024

That's a typo, will update in a sec. The idea is that you modify the properties as the come in, before they're set on the object.

from ember-decorators.

dfreeman avatar dfreeman commented on May 22, 2024

👍 Gotcha, just making sure I understood correctly

from ember-decorators.

rwjblue avatar rwjblue commented on May 22, 2024

We should add some decorators specifically for handling those special component API's (tagName, classNames, classNameBindings, are there others?). Can you open a separate issue for that?

from ember-decorators.

pzuraq avatar pzuraq commented on May 22, 2024

Most of these issue have been addressed in the ES Classes RFC, going to close this in favor of discussion there and fixes upstream in CoreObject

from ember-decorators.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.