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

TypeError: Cannot read property 'empty' of undefined #35

Closed
dhritzkiv opened this issue May 20, 2016 · 0 comments
Closed

TypeError: Cannot read property 'empty' of undefined #35

dhritzkiv opened this issue May 20, 2016 · 0 comments

Comments

@dhritzkiv
Copy link
Member

dhritzkiv commented May 20, 2016

This TypeError halts everything and prevents views from switching.

Came across this issue after upgrading to ampersand-state@5.0.2 (the update which upgraded dependencies, including major bumps to lodash and ampersand-events).


Problem code (at L79):

ViewSwitcher.prototype._onViewRemove = function (view) {
    var emptyCb = this.config.empty; //<- the problem is here. - `config` is undefined. `this` doesn't refer to the ViewSwitcher instance, but the `view`.
   //...
};

which is triggered by (L74):

ViewSwitcher.prototype._registerRemoveListener = function (view) {
    if (view) view.once('remove', this._onViewRemove, this);
};

What's happening in _registerRemoveListener is view.once is being called with three arguments, the last two being the callback and the context. However, somewhere in the chain (I'm guessing ampersand-view -> ampersand-state -> ampersand-events) the third argument is no longer being correctly and the callback isn't being bound to this (the ViewSwitcher instance). Instead, it's being bound to view, which isn't right.

One solution is to change _registerRemoveListener to:

ViewSwitcher.prototype._registerRemoveListener = function (view) {
    if (view) view.once('remove', this._onViewRemove.bind(this)); // note the use of bind
};

I've tested it locally, and it fixes the issue.

I'm happy to file a PR with the change for this module, if all of this makes sense. Can someone review this issue, to make sure I'm not misdiagnosing this?


Edit: I removed some incorrect details from my diagnoses of the problem.

dhritzkiv added a commit to dhritzkiv/ampersand-view-switcher that referenced this issue May 21, 2016
.once no longer accepts a final context argument for `this`, so it must be explicitly `bind`ed

Closes AmpersandJS#35
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