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

handling listeners with listenTo() #231

Closed
galki opened this issue Jul 30, 2015 · 1 comment
Closed

handling listeners with listenTo() #231

galki opened this issue Jul 30, 2015 · 1 comment

Comments

@galki
Copy link

galki commented Jul 30, 2015

I'm confused about the below code and would appreciate any help!

var Layout = Marionette.LayoutView.extend({
    initialize: function() {
        var channel = Backbone.Radio.channel('myChannel');
        // this succeeds (when calling with trigger())
        this.listenTo(channel, 'myTriggerEvent', this.mySetMethod);
        // this also succeeds (when calling with trigger())
        channel.on('myTriggerEvent', this.mySetMethod);

        // this fails (when calling with request())
        this.listenTo(channel, 'myRequestEvent', this.myGetMethod);     
        // this succeeds (when calling with request())
        channel.reply('myRequestEvent', this.myGetMethod);
    },

    mySetMethod: function(value) {
        console.log(value);
    },

    myGetMethod: function() {
        return 'some value';
    }
});

My questions are:

  • Is listenTo preferred or will setting a listener directly on Radio still allow Marionette to remove the listener upon view removal?
  • Does a reply inside a listenTo fail because it's not an on? Will Marionette ever support Radio replies inside listenTo?

I'm using Radio 1.0.0 and Marionette 2.4.1

@jamesplease
Copy link
Member

Is listenTo preferred or will setting a listener directly on Radio still allow Marionette to remove the listener upon view removal?

When a view is destroyed, stopListening is called. stopListening removes any event registered through listenTo, but not on.

Accordingly, if you would automatic event cleanup when the view is destroyed, then you'll want to listenTo your Channels.

Does a reply inside a listenTo fail because it's not an on?

Yes, listenTo is an API of Backbone.Events. Radio Requests are a completely separate thing.

Will Marionette ever support Radio replies inside listenTo?

No, because listenTo is a part of Backbone.Events. See #117 for a discussion of the equivalent for Requests.

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