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

Listen to other events rather than just 'change' #77

Open
tnguyen14 opened this issue Sep 23, 2014 · 0 comments
Open

Listen to other events rather than just 'change' #77

tnguyen14 opened this issue Sep 23, 2014 · 0 comments

Comments

@tnguyen14
Copy link

Currently event bubbling only works for change events (see _getEventBubblingHandler).

In order to emit more custom event types, I had to do the following (notice the else clause):

// base.js
module.exports = State.extend({
    // copy ampersand-state bubble event to support custom event
    _bubbleEvent: function (propertyName) {
        return _.bind(function (name, model, newValue) {
            if (/^change:/.test(name)) {
                this.trigger('change:' + propertyName + '.' + name.split(':')[1], model, newValue);
            } else if (name === 'change') {
                this.trigger('change', this);
            } else {
                this.trigger(name, model, newValue);
            }
        }, this);
    }
});

and then use it like so

// parentView.js
    render: function () {
        this.renderWithTemplate(this);
        this.child = new ChildView();
        this.renderSubview(this.child, this.el);
        this.listenTo(this.child, 'all', this._bubbleEvent('child'));
        return this;
    }

This is sort of a hack, but I can see how this can be useful for other scenarios as well. Would it be good to start supporting more events? It doesn't have to follow the change:property convention, just the event name would be helpful.

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

2 participants