简体   繁体   English

结合Require.js,Backbone.js和服务器端MVC框架

[英]Combining Require.js, Backbone.js and a server side MVC framework

We're actually planning a really complex web application. 我们实际上正在计划一个非常复杂的Web应用程序。 At least for my own standards. 至少对我自己的标准来说。 In the past we have always been using a combination of a server side MVC Framework (Codeigniter) and client side functionality (jQuery and plugins). 在过去,我们一直使用服务器端MVC框架(Codeigniter)和客户端功能(jQuery和插件)的组合。 We have simply written inline javascript code in our views. 我们在视图中简单地编写了内联JavaScript代码。 This worked as expected, but of course with several disadvantages: 这按预期工作,但当然有几个缺点:

  • no caching 没有缓存
  • duplicated js code 重复的js代码
  • maintainability issues 可维护性问题
  • ... ...

My main goal now is to organize the client side code in an efficient and easily maintainable way. 我现在的主要目标是以高效且易于维护的方式组织客户端代码。 But I want to stay with the server side MVC because of the existing know how and some existing interfaces. 但是我想继续使用服务器端MVC,因为现有的知识和一些现有的接口。 Furthermore I want to reduce complex DOM manipulation with jQuery and "spaghetti code". 此外,我想用jQuery和“spaghetti代码”减少复杂的DOM操作。

Now I thought about a combination of Backbone.js and Require.js but I really can't find a tutorial or any solid description about how to combine them with a server side MVC. 现在我想到了Backbone.js和Require.js的组合,但我真的找不到关于如何将它们与服务器端MVC结合起来的教程或任何可靠的描述。 Is it even recommended? 甚至推荐吗?

In my old apps I got a file structure like this: 在我的旧应用程序中,我得到了这样的文件结构:

  • application (CodeIgniter) 应用程序(CodeIgniter)
  • assets 资产
    • js JS
    • css CSS
    • imgs IMGS

Are there any ideas or best practices? 有什么想法或最佳实践吗?

Thank you! 谢谢!

To add to mexique1's advice, it might be worth looking at the backbone-boilerplate project . 要添加mexique1的建议,可能值得查看骨干 - 样板项目 It should provide you best-practice solutions for many of the problems you're currently considering, such as the combination of require and backbone, the organisation of the client-side of your project, and the reduction of complex DOM manipulation (see templating). 它应该为您正在考虑的许多问题提供最佳实践解决方案,例如需求和主干的组合,项目客户端的组织以及复杂DOM操作的减少(请参阅模板) 。

The challenge, as you anticipate, will most likely be in combining the boilerplate approach with the approach you're used to. 正如您所预料的那样,挑战很可能是将样板方法与您习惯的方法相结合。 However, it will almost certainly be worth the effort since it should provide you a solid foundation for this and future projects. 但是,它几乎肯定值得付出努力,因为它应该为这个和未来的项目提供坚实的基础。

I think Backbone is a good choice, and Require is not mandatory here. 我认为Backbone是一个不错的选择,并且Require在这里不是强制性的。

Require will just help you organize your source code and maybe improve performance. 要求只会帮助您组织源代码并可能提高性能。 I think you can start right away with Backbone, which will be the thing you are going to use most, and add Require later. 我认为你可以立即开始使用Backbone,这将是你最常用的东西,并在稍后添加Require。

Regarding Backbone, yes it's easy to use to use its Model with an existing MVC application, provided it returns JSON. 关于Backbone,是的,如果它返回JSON,它很容易使用它与现有的MVC应用程序一起使用它。 To load your existing data you will want to use the fetch method combined to url to adapt to your existing code, or your own method. 要加载现有数据,您需要使用结合到网址fetch方法来适应现有代码或您自己的方法。

Generally, think about which models are displayed in which views. 通常,请考虑在哪些视图中显示哪些模型。 Backbone helps you think this way : I'm displaying Models represented as JSON data in Views which are made by HTML. Backbone可以帮助您这样思考: 我在Views中显示由JIS数据表示的模型。

Also, for the view layer, it's very easy to reuse your existing HTML, because views are not tied to anything, no JavaScript templating or nothing. 此外,对于视图层,重用现有HTML非常容易,因为视图不依赖于任何内容,没有任何JavaScript模板或任何内容。

Simple example : 简单的例子:

<div id="user">
    <span class="name">John</span>
</div>

var UserView = Backbone.View.extend({
    render: function() {
        this.$el('.name').html(this.model.get('name'));
    }
});
var userView = new UserView({el: $('#user')[0], model: ...});

In this example the #user div reflects the state of a User model, with its name. 在此示例中, #user div反映了User模型的状态及其名称。

Also check the Todo App example in Backbone. 另请查看Backbone中的Todo App示例。

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

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