[EDIT: ho risolto il problema in precedenza chiamando delegateEvents(), ma non dovevo farlo. Re-posting w/maggiori informazioni.]eventi backbone.js non attivati dopo il re-rendering
Ho una vista che quando il rendering ha un pulsante di accesso su di esso con un evento click allegato. Per prima cosa esegui il rendering di tutti i lavori: fai clic sul pulsante e al termine della chiamata ajax i messaggi di accesso scompaiono e viene visualizzata la vista di benvenuto (LoggedInView). Tuttavia, se torno a questa vista in un secondo momento (#foo) il rendering dell'interfaccia utente viene rimosso, ma l'associazione eventi non funziona senza forzare manualmente il problema chiamando delegateEents().
Che cosa è successo che i miei eventi non si sono riassociati?
LoginView = Backbone.View.extend({
template: _.template($("#loginTemplate").html()),
initialize: function(stuff,router) {
_.bindAll(this,'render','rc','gotProfile');
this.model.bind("rc",this.rc)
this.router = router;
},
events: {
'click .loginButton': 'login'
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
$(this.el).find(".actionButton").button(); // Button
// this.delegateEvents(this.events); // NEEDED! Re-wire events on re-render
return this;
},
rc: function(code) {
switch(code) {
case errCodes.USER_NOT_FOUND: this.notFound(); break;
case errCodes.OK: this.loginOk(); break;
default: this.otherErr(); break;
}
},
login: function() {
clearErrors($('[rel="req"]'));
var okReq = validate($('#login [rel="req"]'), validateReq);
var okEmail = validate([$('#uid')], validateEmail);
if(okReq && okEmail) {
this.model.set({'uid':$('#uid').val().trim(), 'pwd':$('#pwd').val().trim()});
this.model.fetch();
}
},
notFound: function() {
validate([$('#uid'),$('#pwd')], function(){return[false,"Invalid user/password"]});
},
otherErr: function() {
validate([$('#uid'),$('#pwd')], function(){return[false,"Please contact support for help logging in."]});
},
loginOk: function() {
this.profile = new Profile({uid:this.model.get('uid'),token:this.model.get('key')});
this.profile.bind("rc",this.gotProfile)
this.profile.fetch();
},
gotProfile: function() {
this.router.navigate('/',true);
}
});
LoggedInView = Backbone.View.extend({
template: _.template($("#loggedInTemplate").html()),
uList: new ProfileList(),
initialize: function() {
_.bindAll(this,'render','renderUserList','renderUser');
this.model.bind('show', this.render);
this.uList.bind('rc', this.render);
},
events: {
'click #ufa': 'getUsersForHouse'
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
this.renderUserList();
// this.delegateEvents(this.events); // NEEDED! Re-wire events on re-render
return this;
},
renderUserList: function() {
$(this.el).find('ul').empty();
this.uList.each(this.renderUser);
},
renderUser: function(aUser) {
$(this.el).find('#userList').append("<li>"+aUser.get('person').firstName+"</li>");
},
getUsersForHouse: function() {
this.uList.fetch(this.model.get('token'),"house");
}
});
Main = Backbone.Router.extend({
routes: {
'foo': 'foo',
'*all': 'home'
},
initialize: function() {
this.token = new Token();
this.loginView = new LoginView({model:this.token},this);
},
foo: function(){ // naving here looses click event on login button
$('#login').empty();
$("#login").append(this.loginView.render().el);
},
home: function() {
$('#login').empty();
if(this.loginView.profile == null)
$("#login").append(this.loginView.render().el);
else {
this.loggedInView = new LoggedInView({model:this.loginView.profile});
$("#login").append(this.loggedInView.render().el);
}
}
});
è necessario fornire ulteriori informazioni per sapere cosa sta succedendo e perché. puoi pubblicare più codice della tua vista e il tuo codice router? –
Ecco il mio router e interessata Vista: main = Backbone.Router.extend ({ \t percorsi: { \t \t '': 'casa', \t \t 'ok': 'loggedin' \t}, \t initialize: function() { \t \t this.token = nuovo token(); \t \t this.loginView = new LoginView ({modello: this.token}, this);} \t, \t casa: function() { \t \t $ ('# login'). Empty(); \t \t $ ("# login"). Append (this.loginView.render(). El); \t}, \t loggedin: function() { \t \t this.loggedInView = new LoggedInView ({modello: this.loginView.profile}); \t \t $ ('# login'). Empty(); \t \t $ ("# login"). Append (this.loggedInView.render(). El); \t} }); – Greg
Non riesco ad aggiungere la vista, troppo grande per essere accettata da questo editor. LoginView ha la clausola di eventi. – Greg