Backbone.js - 协调布局(实例化)、状态和历史
我正在寻找一种简单且可扩展的方法来序列化 Backbone.js 应用程序的完整状态。
假设我想存储完整状态(一些小部件模型 + 元信息,例如app 等)在我的“GlobalStateModel”中:
var GlobalStateModel = Backbone.Model.extend({
defaults : {
layout : undefined,
widgetType1Collection : undefined, //Widget models
},
});
我想这样做,因为这样我就可以轻松设置相应的历史记录条目:
var HistoryController = Backbone.View.extend({
model : globalStateModel,
initialize: function () {
_.bindAll(this, "render", "updateHistory");
this.model.bind("change", this.updateHistory);
},
updateHistory : function () {
var globalStateModel = this.model.toJSON()
var state = jQuery.param.querystring("", state )
appRouter.navigate('v1' + state, false);
}
});
但是这里出现了棘手的部分......我想要一个 LayoutController 或类似的实例来实例化与上面:
var LayoutController = Backbone.View.extend({
initialize: function () {
_.bindAll(this, "render");
this.model.bind("change:layout", this.render);
},
render : function () {
var layout = this.model.get("layout");
switch(layout){
case "layout-1":
this.renderLayout1();
break;
case "layout-2":
this.renderLayout2();
break;
}
},
renderLayout1 : function () {
var widgetModel1 = new WidgetModel()
var widgetController1 = new WidgetController({model : widgetModel1})
this.model.widgetType1Collection.add(widgetModel1);
//fetch the right widget model attributes from the global state?
widgetAttrs = { ..... }
widgetModel1.set(widgetAttrs)
},
});
也许这种方法是完全有缺陷的,或者也许我只是缺少一些管道来使其正常工作?
如何让 LayoutController 实例化 1 个或多个小部件并让它们的模型成为 globalState 的一部分?
因此,1)当他们的模型/集合发生变化时,globalState会正确序列化,2)globalState更改/哈希更改会正确反映在小部件中吗?
I am looking for a simple and extensible way of serializing the full state of my Backbone.js application.
Let's say I want to store the full state (some widget models + meta information such as the layout of the app etc.) in my "GlobalStateModel":
var GlobalStateModel = Backbone.Model.extend({
defaults : {
layout : undefined,
widgetType1Collection : undefined, //Widget models
},
});
I want to do this because then I can easily set corresponding history entries:
var HistoryController = Backbone.View.extend({
model : globalStateModel,
initialize: function () {
_.bindAll(this, "render", "updateHistory");
this.model.bind("change", this.updateHistory);
},
updateHistory : function () {
var globalStateModel = this.model.toJSON()
var state = jQuery.param.querystring("", state )
appRouter.navigate('v1' + state, false);
}
});
But here comes the tricky part ... I want to have a LayoutController or similar that instantiates widget modules that works together with the above:
var LayoutController = Backbone.View.extend({
initialize: function () {
_.bindAll(this, "render");
this.model.bind("change:layout", this.render);
},
render : function () {
var layout = this.model.get("layout");
switch(layout){
case "layout-1":
this.renderLayout1();
break;
case "layout-2":
this.renderLayout2();
break;
}
},
renderLayout1 : function () {
var widgetModel1 = new WidgetModel()
var widgetController1 = new WidgetController({model : widgetModel1})
this.model.widgetType1Collection.add(widgetModel1);
//fetch the right widget model attributes from the global state?
widgetAttrs = { ..... }
widgetModel1.set(widgetAttrs)
},
});
Maybe this approach is completely flawed or maybe I'm just missing some plumbing to get this working?
How would you make the LayoutController instantiate 1 or several widgets and have their models be part of the globalState?
So that 1) when their models/collection changes, the globalState is serialized properly and 2) that globalState changes/hashchanges are reflected properly in the widgets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论