使用 Backbone.js 渲染布局
例如,如果您要使用 Backbone.js 和 jQuery 构建单页 Web 应用程序 (SPWA),并使用两个控制器,每个控制器都需要唯一的页面布局,那么您将如何呈现该布局?
- ControllerA 是三列布局。
- ControllerB 是两列布局。
- 默认路由激活 ControllerA.Welcome()——初始渲染。
- 两个控制器在其列中呈现不同的视图,充分利用 Backbone.js 模型/视图的所有优势。
问题
当用户请求映射到ControllerB的路由时,整个页面布局需要更改为不再使用ControllerA布局。这将隐藏 ControllerA 的布局并显示 ControllerB 的布局——或者,渲染布局(如果尚未在 DOM 中)。
我的第一个想法
您会使用 Backbone.js 视图来渲染布局,然后使用其模型绑定视图来渲染每列吗?
我的第二个想法
您是否会向使用 jQuery 呈现布局的控制器添加一个设置/布局方法,然后允许负责路由的操作执行此操作?在控制器中使用 jQuery 对我来说有点不舒服,但是,我希望控制器负责确保正确的布局对其路由可见。
以下是我的第二个想法的片段:
var Controller = Backbone.Controller.extend
({
routes :
{
"" : "welcome" // default action
}
/** Constructor **/
,initialize: function(options)
{
console.log('Workspace initialized');
}
// LAYOUT
,renderLayout : function ()
{
console.log('Rendering Layout.');
var $ = window.$;
var layout = require('js/layout/app/big_menu');
$(layout.parent).html(layout.html);
}
// ACTIONS
/** Default Action **/
,welcome : function ()
{
this.renderLayout();
console.log('Do the whole model/view thing...');
}
});
谢谢
感谢您花时间回复。我很感激!
If you were to build a single page web application (SPWA) using Backbone.js and jQuery with--for example--two controllers that each required a unique page layouts, how would you render the layout?
- ControllerA is a three column layout.
- ControllerB is a two column layout.
- The default route activates ControllerA.Welcome() -- the initial rendering.
- Both controllers have different views rendered within their columns that take advantage of all the Backbone.js model/view goodness.
The Problem
When the user requests a route mapped to ControllerB, the entire page layout needs to change to no longer use the ControllerA layout. This would hide ControllerA's layout and show ControllerB's layout -- or, render the layout if not already in the DOM.
My First Thought
Would you use a Backbone.js view to render the layout, and then, render each column with it's model-bound views?
My Second Thought
Would you add a setup/layout method to your controller that used jQuery to render the layout and then allow the action responsible for the route do it's thing? Using jQuery within the controller feels a little off to me, but, I want the controller to be responsible for ensuring the right layout is visible for it's routes.
Here is a snippet for my second thought:
var Controller = Backbone.Controller.extend
({
routes :
{
"" : "welcome" // default action
}
/** Constructor **/
,initialize: function(options)
{
console.log('Workspace initialized');
}
// LAYOUT
,renderLayout : function ()
{
console.log('Rendering Layout.');
var $ = window.$;
var layout = require('js/layout/app/big_menu');
$(layout.parent).html(layout.html);
}
// ACTIONS
/** Default Action **/
,welcome : function ()
{
this.renderLayout();
console.log('Do the whole model/view thing...');
}
});
Thank You
Thanks for taking the time to respond. I appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我倾向于同意 Julien 的观点——尽可能保持布局无状态是件好事。所有内容总是以骨架形式布置在页面上,至少是这样。当需要显示特定的布局或配置时,您可以延迟渲染其内容,并使用 CSS 显示 UI 的该部分。互斥的 CSS 类对此很有用,例如:“projects-open”、“documents-open”、“notes-open”。
I tend to agree with Julien -- it's nice to keep your layouts as stateless as possible. Everything is always laid out on the page, in skeleton form, at least. When the particular layout or configuration needs to be displayed, you lazily-render its contents, and display that portion of the UI with CSS. Mutually-exclusive CSS classes are useful for this, things like: "projects-open", "documents-open", "notes-open".
我更喜欢已经在页面上布置应用程序的框架。因此,您拥有页面上不同元素的完整布局,并针对这些元素创建主干视图,以便它们正确布局。
当你有一个布局时,这很有效,当你有多个布局时,事情会变得有趣。您可以将所有布局放在页面上,并根据您的逻辑隐藏不同的配置。您可以看到布局是层次结构的初始视图。因此,您渲染布局,然后加载视图。
没有真正的一种方法可以做到这一点。每个都有优点和缺点。我不会做的一件事是在控制器中渲染布局。我将所有渲染和 html 放在视图中,这样我就可以处理控制器和模型上的逻辑(这里考虑 MVC)。
I prefer to have the skeleton of the application laid out on the page already. So you have the complete layout with the different elements on the page and you create your backbone view against those elements so they are correctly laid out.
This works well when you have a single layout, things get fun when you have multiple. You could put all layouts on the page and hide the different configurations depending on your logic. You can see the layout has being the initial view of an hierarchy. So you render the layout and then have the views load.
There is no real one way of doing this. There are pros and cons for each. One thing I would not do is render the layout in the controller. I put all rendering and html in views so I can deal with logic on the controller and model (think MVC here).
我正在使用backbone.js 设计一个基于模块的内联网系统,并且我基本上在文档加载上使用以下算法。
moduleController 都在 init 时获得对 appController 的访问权限。当捕获哈希位置时,它们将 pageChange 事件发送到包含 pageManifest 对象的 appController。 pageManifest 对象包含设置各个视图所需的所有信息,例如面包屑信息、标题信息,最重要的是对实例化 contentView 的引用。 appController使用pageManifest中的信息来设置不同的持久视图,删除contentContainer中以前的contentView并将模块提供的contentView插入到容器中。
这样,不同的设计人员可以在不同的模块上工作,他们所需要知道的只是 pageManifest 对象的规范以及 contentView 的外观。他们可以建立自己的复杂路由系统,使用自己的模型和定制的 contentView(尽管我们计划有一个 listViews、objectViews 等库来继承)。
我们现在正处于设计阶段,所以我不能真正保证这是我们最终使用的设计,或者我们没有在其中发现任何漏洞,但从概念上讲,我们认为它是合理的。评论?
I'm designing a module-based intranet system using backbone.js and I basically use the following algorithm on document load.
The moduleControllers all gain access to the appController on init. When catching a hash-location, they send a pageChange event to the appController containing a pageManifest object. The pageManifest object contains all the information needed to set the respective views, such as breadcrumbs info, header info, and most importantly, a reference to an instantiated contentView. The appController uses the information in the pageManifest to setup the different persistent views, deletes the former contentView in the contentContainer and inserts the contentView provided by the module into the container.
This way, different designers can work on different modules and all they have to know is the specification of the pageManifest object and how the contentView should look. They can set up complex routing systems of their own, use their own models and customized contentViews (though we plan to have a library of listViews, objectViews, etc to inherit from).
We're at the design phase right now, so I can't really guarantee that this is the design we'll finally use or that we don't find any holes in it, but conceptually, we think it's sound. Comments?
无论 Backbone 还是任何其他 js 框架/库,我都遇到完全相同的问题。
想象一下,您有一个 SIGN IN FORM 视图,它需要单列布局,并且您将该视图注入到该单个 div 中。
然后,一旦登录成功,就会以某种方式呈现另一个布局(假设有一个 HEADER 区域、FOOTER 区域、LEFT 区域,然后是 MAIN 区域(右列)用于其他所有内容。
标题可能包含 LOGO 视图(如果它具有功能)并且全局/用户菜单视图。左侧区域将包含主导航视图。
插入的其他视图使用。
主导航视图中的每个链接都会加载一个新的子布局,以供我不想 常规控制器/视图关心当前呈现的布局,只是它们的容器元素存在并且准备好注入,
我考虑以一种聪明的方式使用路由(不是传统意义上的),例如
:路线是“/section1/whatever/sub/page/within/section/1”,“正则表达式代表除sign_in之外的任何内容”和“/section1/*”上方的两个路线匹配器都将运行,这意味着主要布局将是渲染,然后如果有意义的话,将渲染section1子布局。
然后所有其他普通控制器都使用传统意义上的路由。
需要有一种很好的方法来管理布局并确保这些布局、子布局和视图被安全地拆除,以确保内存泄漏等原因得到处理。
很想听到有人设计并实现了一些优雅的东西。
I am having the exact same issue regardless of Backbone or any other js framework/library.
Imagine you have a SIGN IN FORM view which requires a single column layout and you inject the view into that one single div.
Then once signed in successfully, somehow another layout is rendered (lets say a HEADER zone, FOOTER zone, LEFT zone and then the MAIN zone (right column) for everything else.
The header might contain a LOGO view (if it has functionality) and a GLOBAL/USER MENU view. The LEFT zone will contain the PRIMARY NAV view.
Then a further complexity.; Each link inside the PRIMARY NAV view loads up a new sub layout ready for further views to inject themselves into.
I don't want the regular controllers/views to care about what layout is currently rendered, just that their container element exists and is ready to be injected into.
I thought about using routes (not in the traditional sense) in a clever way something like:
Meaning that if the route was "/section1/whatever/sub/page/within/section/1" the two route matchers above "regex to represent anything but sign_in" and "/section1/*" would both run, meaning that the primary layout would be rendered and then the section1 sub layout would be rendered after if that makes sense.
Then all other normal controllers use routes in the traditional sense.
There needs to be a nice way to manage layouts and ensure those layouts, sub layouts and views are torn down safely to ensure memory leaks are handled amongst other reasons.
Would love to hear someone that has designed and implemented something elegant.