Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify default attribute overriding with mixins #352

Open
ZhihaoJia opened this issue Jun 10, 2015 · 1 comment
Open

Clarify default attribute overriding with mixins #352

ZhihaoJia opened this issue Jun 10, 2015 · 1 comment

Comments

@ZhihaoJia
Copy link

The mixin API documentation has a section on overriding defaults:

The attributes method is available to both component and mixin modules. When used with mixins it will not overwrite attributes already defined in the component module.

My impression was that default attributes defined in components would never be overridden by default attributes defined in mixins. However, the opposite seems to be the case for me:

module.exports = withCookies;

function withCookies() {
  this.attributes({
    wantsCookies: false
  });
}
module.exports = flight.component(cookieMonster, withCookie);

function cookieMonster() {
  this.attributes({
    wantsCookies: true
  });

  this.after('initialize', function initialize() {
    console.log(this.attr.wantsCookies); // false
  });
}

If I want the component's default attribute to take precedence over the mixin's, I need to change the order they are passed into flight.component():

module.exports = flight.component(withCookie, cookieMonster);

function cookieMonster() {
  this.attributes({
    wantsCookies: true
  });

  this.after('initialize', function initialize() {
    console.log(this.attr.wantsCookies); // true
  });
}

Is this the intended behavior? If so, can we clarify this in the documentation?

@giuseppeg
Copy link
Contributor

@ZhihaoJia that's correct.
The old this.defaultAttrs would fail when the second mixin had an attribute already defined on the previous mixins:

Uncaught Error: utils.push attempted to overwrite "wantsCookies" while running in protected mode

When the docs were updated Nicolas forgot to invert the order I guess.

Here is the relevant code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants