Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  128] [ 1]  / answers: 1 / hits: 15331  / 13 Years ago, sun, february 19, 2012, 12:00:00

I'd like to find a way to raise a backbone.js event without something having changed in the model or in the dom.



For instance, I'm loading the Facebook SDK asynchronously. I'm subscribed to the auth.login event, and would like to send a message to my view that the user has logged in so it can re-render itself appropriately.



My view looks similar to this:



window.CreateItemView = Backbone.View.extend({
el: $('#content'),
initialize: function() {
this.render();
},
render: function() {
// do something
return this;
},
setSignedRequest: function(signedRequest) {
//do something with signedRequest
}
});


In my facebook code, I do this:



  FB.Event.subscribe('auth.login', function(response){
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
window.signedRequest = response.authResponse.signedRequest;

if (window.view && window.view.setSignedRequest) {
window.view.setSignedRequest(window.signedRequest);
}
}
});


However, while window.view exists, it cannot see the setSignedRequest method. I've ensured that my scripts are loading in the correct order. Strangely I have this same code on a different page albeit a different View object and it works fine. I haven't seen any difference that would account for this.



A better solution would be to raise some sort of event and have the view listen for it. However, I don't want to utilize the change event on the model as the signedRequest shouldn't be a property of the model. Is there a better way of accomplishing this?


More From » backbone.js

 Answers
5

Backbone.View is extended with Backbone.Events which means you can easily trigger custom events and pass whatever data you want



var View = Backbone.View.extend({

initialize: function() {

this.on('customEvent', this.doSomething, this);
}

doSomething: function(someData) {

// this!
}
});

var view = new View();

view.trigger('customEvent', someDataHere);


though I don't know why you can't see the method on the view - it should work - Are you 100% sure you are instantiating the view correctly? and that window.view is instance of CreateItemView ?


[#87362] Saturday, February 18, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aman

Total Points: 341
Total Questions: 92
Total Answers: 92

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;