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

onEnter defined in constructor's prototype not firing #3293

Closed
kyse opened this issue Jan 24, 2017 · 3 comments
Closed

onEnter defined in constructor's prototype not firing #3293

kyse opened this issue Jan 24, 2017 · 3 comments
Milestone

Comments

@kyse
Copy link

kyse commented Jan 24, 2017

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.

@christopherthielen
Copy link
Contributor

christopherthielen commented Jan 24, 2017

I'll see if we can enable the old behavior.

We switched from this:

  function registerState(state) {
    // Wrap a new object around the state so we can store our private details easily.
    state = inherit(state, {
      self: state,
      resolve: state.resolve || {},
      toString: function() { return this.name; }
    });

to this:

    StateQueueManager.prototype.register = function (config) {
        var state = inherit(new State(), extend({}, config, {
            self: config,
            resolve: config.resolve || [],
            toString: function () { return config.name; }
        }));

The extend() is like Object.assign and only processes "own properties"

@christopherthielen christopherthielen modified the milestones: 1.0.0-final, 1.0.0-rc.2 Jan 24, 2017
@christopherthielen
Copy link
Contributor

Switched back to use the state declaration as the prototype for rc.2

@kyse
Copy link
Author

kyse commented Jan 30, 2017

Sweet!

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