[編輯:我通過調用delegateEvents()解決了前面的問題,但不應該。重新張貼瓦特/更多信息。]backbone.js事件在重新渲染後沒有觸發
我有一個視圖,當呈現有一個登錄按鈕,並附加一個點擊事件。首先渲染所有作品:單擊按鈕,一旦成功調用ajax,登錄提示消失並顯示歡迎視圖(LoggedInView)。但是,如果稍後導航回到此視圖(#foo),UI將呈現,但事件關聯不會通過調用delegateEents()手動強制解決問題。
發生了什麼,我的事件沒有重新聯繫自己?
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);
}
}
});
您需要提供更多信息以瞭解正在發生的事情以及原因。你能發佈更多的視圖代碼和你的路由器代碼嗎? –
這裏是我的路由器和受影響的視圖:主要= Backbone.Router.extend({ \t路線:{ \t \t '': '家', \t \t 'OK': '的loggedIn' \t}, \t初始化:函數(){ \t \t this.token =新令牌(); \t \t this.loginView =新LoginView({模型:this.token},這一點); \t}, \t家:功能(){ \t \t $('#login')。empty(); \t \t $(「#login」)。append(this.loginView.render()。el); \t}, \t的loggedIn:函數(){ \t \t this.loggedInView =新LoggedInView({模型:this.loginView.profile}); \t \t $('#login')。empty(); \t \t $(「#login」)。append(this.loggedInView.render()。el); \t} }); – Greg
我無法添加視圖 - 對於此編輯器來說太大了,無法接受。 LoginView具有事件子句。 – Greg