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

State objects cannot be classes #34

Closed
christopherthielen opened this issue Jan 29, 2017 · 0 comments
Closed

State objects cannot be classes #34

christopherthielen opened this issue Jan 29, 2017 · 0 comments
Milestone

Comments

@christopherthielen
Copy link
Member

In angular-ui-router 0.x you could create a class for a state:

class ModalState implements StateDeclaration {
  constructor(def) {
    Object.assign(this, def);
  }
  onEnter() {
    this.modalInstance = $uibModal.open(...);
    thos.modalInstance.result.then(this.onClose.bind(this), this.onDismiss.bind(this));
  }
}

This worked fine back then because the internal State object's prototype was the user supplied StateDeclaration, i.e,

var internalState = Object.create(stateDeclaration)`

but this is no longer possible because we now use new State() to get the State (class) prototype, i.e.,

var internalState = Object.assign(new State(), stateDeclaration);

Since Object.assign only makes copies of the stateDeclaration's own properties, it does not make copies of the prototype onEnter function, for example.


We should switch back to the old behavior (internal state object's prototype is the user supplied state declaration). We will lose the ability to do obj instanceof State, but that is a small price to enable numerous other convenient features.

For example, hook functions can check for user-supplied state properties without explicitly querying state.self.property:

transitionService.onStart({ to: state => state.customflag }, ...)

Projects like sticky states can define new callbacks (like onInactivate) without also creating a statebuilder to enable access to the stateDeclaration.onInactivate property.

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

No branches or pull requests

1 participant