'Backbone/Parse events scope
I am triggering a custom event, but can't find a way to bind an action to it from another file. I guess it is a scope issue but I just can't resolve it alone.
In my app entry file, I am triggering a custom boom event like this :
var page_object_test = {};
_.extend(page_object_test, Parse.Events);
page_object_test.trigger("boom");
This piece of code is contained within a jQuery $(document).ready() statement.
And then, from within a jQuery plugin heald in a different file, and enclosed within a (function($) {})(jQuery); construct, I want to bind some function to my boom event, but nothing happens :
var page_object = {
update_page_title : function(route, params) {
var new_title = "";
switch(route) {
default:
new_title = "La meilleure façon d'emmener vos enfants à l'école";
}
document.title.html(new_title);
}
};
_.extend(page_object, Parse.Events);
page_object.on("boom", function() {console.log(".on() binding is working!")});
Why can't I receive this event from another file? I realize I don't get how do Backbone events bubble up. More than the right way to write this, i would really love to be pointed out toward a clear explanation of the scope of those events.
Any explanation is most welcome
Solution 1:[1]
Have you considered Marionette.Wreqr
https://github.com/marionettejs/backbone.wreqr
It's used by default in Marionette, but can also be used independently outside.
Otherwise you can just use Backbone.Events as it's described in Backbone documentation
var object = {};
_.extend(object, Backbone.Events);
object.on('expand', function(){ alert('expanded'); });
object.trigger('expand');
assuming that object is available in global scope, you can subscribe and trigger events on it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
