简体   繁体   English

在非SPA(单页应用程序)中使用Require.js和Backbone

[英]Using Require.js and Backbone in non SPA (Single Page Application)

I am currently working on a new Java project which relies on Spring MVC architecture (classical front controller-> dispatcher-> models and jsp views). 我目前正在开发一个新的Java项目,该项目依赖于Spring MVC架构(经典的前端控制器->调度程序->模型和jsp视图)。 However, as most of our views need Ajax dynamicity (jQuery), I am willing to use Require.js and Backbone.js frameworks mainly to structure the JavaScript code. 但是,由于我们大多数视图都需要Ajax动态性(jQuery),因此我愿意使用Require.js和Backbone.js框架来主要构造JavaScript代码。

The problem is that, since my application is not SPA, I will have to load the js resources for each page request therefore losing state in JavaScript. 问题在于,由于我的应用程序不是SPA,因此我将不得不为每个页面请求加载js资源,因此会丢失JavaScript中的状态。 That means that I will somehow have to let alone the Router and Model capabilities of Backbone which make no sense to that effect. 这意味着我将不得不以某种方式不让Backbone拥有Router和Model功能,而这种功能没有任何意义。

In that case, would it still be necessary to use Backbone in such a scenario? 在这种情况下,在这种情况下是否仍需要使用Backbone? Is Require.js advised in such an architecture? 在这种架构中建议使用Require.js吗? Or will I be better off giving up these two technologies which are a perfect fit for SPA mainly. 或者我最好放弃这两种最适合SPA的技术。 Has anybody had experience with it (off-SPA)? 有没有人有经验(非SPA)?

Thanks a bunch, Jimmy 谢谢你,吉米

I have been using Backbone and require on a Spring MVC app for a while now. 我一直在使用Backbone,并要求在Spring MVC应用程序上使用一段时间。 while the Spring controller returns the view, the backbone router is the one that takes care of starting the right backbone views, which inside of them I handle a lot of JavaScript code like validations, popup saving and fetching data etc. While this kind of architecture is hybrid I think it worth's the effort. 当Spring控制器返回视图时,骨干路由器是负责启动正确的骨干视图的人,其中我处理了许多JavaScript代码,例如验证,弹出窗口保存和获取数据等。是混合动力,我认为值得努力。 insert in the header of your htmls the same call for the require main file, so you will have only a js line in your HTML files. 在htmls的标头中插入对require主文件的相同调用,因此HTML文件中只有js行。 then inside of main start your backbone routers, you will perhaps need to add #+something in your urls in order to have the backbone ruter(s) to work. 然后在主要启动骨干路由器的内部,您可能需要在URL中添加#+内容,以使骨干标准运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM