Gri*_*rim 1 backbone.js webpack webpack-dev-server webpack-hmr
我从这里使用了一个示例项目来设置一个带有热模块替换的webpack项目.然后我建立了一个示例骨干应用程序.
// main.js
import $ from 'jquery';
import Backbone from 'backbone';
import Router from './router';
window.app = window.app || {};
const app = new Backbone.Marionette.Application();
app.addRegions({content: '#content'});
app.on('start', () => {
if (Backbone.history)
Backbone.history.start({ pushState: true })
}
);
app.addInitializer(() => {
return new Router();
});
$( () => { app.start() });
// HMR
if (module.hot) {
module.hot.accept();
}Run Code Online (Sandbox Code Playgroud)
我可以看到HRM根据[HMR] connected调试输出正在加载.当文件发生变化时,我可以看到它根据以下输出重建并推送到客户端的更新:
[HMR] Updated modules:
process-update.js?e13e:77 [HMR] - ./app/backbone/views/template.hbs
process-update.js?e13e:77 [HMR] - ./app/backbone/views/hello.js
process-update.js?e13e:77 [HMR] - ./app/backbone/router.js
Run Code Online (Sandbox Code Playgroud)
但是屏幕不会重新加载.什么都没发生.
知道如何让这个工作吗?或HMR应该只与React一起使用?
小智 5
这有点小问题,但你可以让它与骨干一起工作.一篇博客文章在 这里解释得相当不错.(免责声明,我写了)
简而言之,您需要明确告诉您的父视图您可以接受热重新加载,然后重新启动require新的热重新加载视图,关闭现有的子视图,然后重新呈现它.以下示例使用了Ampersand,但相同的基本原则适用于Marionette或vanilla 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;
}
});
Run Code Online (Sandbox Code Playgroud)
```
| 归档时间: |
|
| 查看次数: |
1159 次 |
| 最近记录: |