这有点诡计,但你可以用脊梁骨来完成。博客文章是
here that explains it fairly well
。(免责声明,我写的)
简而言之,您需要明确地告诉您的父视图您可以接受热重新加载,然后您就可以-
require
新的热重新加载视图,关闭现有的子视图,然后重新渲染。下面的例子使用了Ampersand,但同样的基本原理也适用于Marionette或香草Backbone
/* parent.view.js */
var ChildView = require('./child.view.js');
var ParentView = AmpersandView.extend({
template : require('path/to/template.hbs')
initialize: function(){
var self = this;
if(module.hot){
module.hot.accept('./child.view.js', function(){
// re-require your child view, this will give you the new hot-reloaded child view
var NewChildView = require('./child.view.js');
// Remove the old view. In ampersand you just call 'remove'
self.renderChildView(NewChildView);
});
}
},
renderChildView(View){
if(this.child){
this.child.remove();
}
// use passed in view
var childView = new View({
model: this.model
});
this.child = this.renderSubview(childView, this.query('.container'));
}
render: function(){
this.renderWithTemplate(this);
renderChildView(ChildView);
return this;
}
});
```