我对Backbone相对较新,我遇到了这个问题。
我在DustJS上使用Backbone
我的模板看起来像这样 - index.dust
<div id="parentView">
<div class="section">
{>"app/inc/responseMessage" /}
<div class="userDetails">
{! A button to get user details !}
</div>
</div>
</div>
这是我的部分 - responseMessage.dust
<div id="responseMessage">
{@eq key="{data.success}" value="true"}
<div class="alert alert-success success" role="alert">success</div>
{/eq}
</div>
我的JS看起来像这样
initialize: function() {
this.responseMessageView = this.responseMessageView ||
new ResponseMessageView({
model: new Backbone.Model()
}); // View is for the partial
this.model = this.model || new Backbone.Model(); //View for the whole page
},
当事件发生时调用下面的函数,它执行POST并成功返回。
primaryViewEventTrigger: function(event){
//Button click on `index.dust` triggers this event and does a POST event to the backend
this.listenToOnce(this.model, 'sync', this.primaryViewSuccess);//this will render the whole view.
this.listenToOnce(this.model, 'error', this.primaryViewError);
this.model.save({data: {x:'123'}});
}
responseViewEventTrigger: function(event){
//Button click on `responseMessage.dust` triggers this event and does a POST event to the backend
this.listenToOnce(this.responseMessageView.model, 'sync', this.responseViewSuccess);//it should only render the partial view - responseMessage.dust
this.listenToOnce(this.responseMessageView.model, 'error', this.primaryViewError);
this.responseMessageView.model.save({data: {x:'123'}});
}
primaryViewSuccess: function(model,response){
this.model.set('data', response.data);
this.render();
}
responseViewSuccess: function(model,response){
this.responseMessageView.model.set('data', response.data);
console.log(this.responseMessageView.model);
this.responseMessageView.render(); // Does not work in some cases
}
我的回调函数的实现
exports.sendEmail = function sendEmail(req, res){
req.model.data.success = true;
responseRender.handleResponse(null, req, res);
};
this.model
属于整个页面的模型。而 this.responseMessageView.model
是部分的模型。
问题:在大多数情况下,这完全正常。有一种情况是它不会使用最新的模型值渲染部分。当我点击按钮时 index.dust
和 primaryViewSuccess
被执行。之后我点击另一个按钮并触发 responseViewEventTrigger
。它成功地完成了POST,它来了 responseViewSuccess
并将其存储在模型中。但它没有在前端显示出来。 data.success
仍然不是真的 console.log(this.responseMessageView.model)
显示 attributes->data->success = true
但是当我刷新页面时它的行为完全相同。只是当它 primaryViewSuccess
被召唤然后 responseViewSuccess
它没有采取最新的模型变化。 换句话说,模型正在更新,但DOM保持不变。
我在这里想念的是什么?谢谢你的时间!