라우터에 addPost
기능이 있습니다. 나는 postAddView
에게 함수가 호출 될 때마다 다시 생성하지 않으 :Backbone.js - 다른보기를 렌더링 한 후에 이벤트 트리거가 작동하지 않습니다.
addPost: function() {
var that = this;
if (!this.postAddView) {
this.postAddView = new PostAddView({
model: new Post()
});
this.postAddView.on('back', function() {
that.navigate('#/post/list', { trigger: true });
});
}
this.elms['page-content'].html(this.postAddView.render().el);
}
다음은 PostAddView입니다 :
PostAddView = backbone.View.extend({
events: {
'click #post-add-back': 'back'
}
, back: function (e) {
e.preventDefault();
this.trigger('back');
}
});
postAddView이 렌더링 처음, 이벤트 트리거가 잘 작동합니다. 그러나 다른보기를 page-content
으로 렌더링하고 postAddView
을 다시 렌더링하면 이벤트 트리거가 더 이상 트리거되지 않습니다. 그래도 addPost
버전이 제대로 작동합니다.
addPost: function() {
var that = this, view;
view = new PostAddView({
model: new Post()
});
this.elms['page-content'].html(view.render().el);
view.on('back', function() {
delete view;
that.navigate('#/post/list', { trigger: true });
});
}