Skip to content

onEnter defined in constructor's prototype not firing #3293

Closed
@kyse

Description

@kyse

So, I'm probably not doing things properly, but figured I'd post this to get some feedback just incase this case wasn't thought about.

Background: I have my states defined in an API, so when the app loads up, I grab the states from the api, then build out an array of state objects to pass along to the router via:

angular.forEach(routes, function(value, name) {
  $stateProvider.state(name, value);
}

This seems to work fine, so I wont dwell on this process. The problem though is I build out a custom predefined state that includes onEnter, onExit methods, etc for a modal popup (ie I've bound a specific state I define in my API as a modal state, I want the template to be wrapped and loaded within a modal window). So when my routes builder sees a route that's flagged as modal, it passes the needed state data into a constructor function (from a factory in the modal module).

End result, I get a state object to pass to $stateProvider.state. This object was new'd to generate it.

function ModalState(apiData) {
  this.url = apiData.url;
  this.parent = apiData.parent;
  this.abstract = !!apiData.abstract;
  this.modalInstance = null;
}

ModalState.prototype.onEnter = function onEnter() {
  this.modalInstance = $uibModal.open(...);
  ths.modalInstance.result.then(this.onClose.bind(this), this.onDismiss.bind(this)); // bind modal closing to route changes if needed...
};

myState = new ModalState(apiData);

Now the funky part that I may be doing wrong here, I've added the onEnter, onExit methods as a prototype of the modal state constructor. So when I pass in the state object to the stateProvider, the onEnter hook is in the prototype of the object. This use to work fine before I updated to 1.0-rc1, but it seems now (I'm guessing) it does a hasOwnProperty for onEnter, instead of checking if onEnter is a function? So, the onEnter method isn't called for my modal state anymore.

Now of course, I can just rework my factory to build out the state object without using prototypes and new'ing a constructor method anymore. But just figured I'd point this out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions