Skip to content

State objects cannot be classes #34

Closed
@christopherthielen

Description

@christopherthielen

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions