【问题标题】:Marionette Regions and routing木偶区域和路由
【发布时间】:2015-06-09 13:19:13
【问题描述】:
我正在使用 LayoutView 以表格形式显示集合。当用户单击 tr 时,我将 CompositeView 交换为使用同一区域显示详细信息的 ItemView。除了后退按钮的功能被破坏之外,这一切都有效。有没有办法捕获返回事件并切换视图?
或者我应该使用两个视图并传递模型 id 然后重新获取模型?但是,问题在于额外的请求,除非我使用本地存储,否则我会丢失表的过滤器和排序值。
【问题讨论】:
标签:
backbone.js
marionette
backbone-views
【解决方案1】:
包含更多代码会更好,但无论如何我都会尝试为您的问题提供一些指导。
为避免两次获取数据,您可以在“父”组件中保留一个公共对象,例如在路由器中。
var theObject;
var router = Marionette.AppRouter.extend({
routes: {
"routeA/:id": "goToRouteA",
"routeB/:id": "goToRouteB"
},
goToRouteA: function(id) {
MyRegion.show(new myLayout({
model: this._getCommonObject(id)
}));
},
goToRouteB: function(id) {
MyRegion.show(new myLayout({
model: this._getCommonObject(id)
}));
},
/*Return the common object for the views*/
_getCommonObject: function(id) {
theObject = (theObject && theObject.get('id') == id) ? theObject : MyApp.request('getTheObject');
return theObject;
}
});
这样,您可以保持对同一对象的引用而不会丢失信息。
您只需确保在不需要对象时将其删除,以避免保留旧信息,例如在区域关闭事件中。