我试图从其网站http://documentcloud.github.com/backbone上了解backbone.js的实用功能,但我仍然搞不懂太多。

谁能帮我解释一下它是如何工作的,以及它如何有助于编写更好的JavaScript?


当前回答

是一个MVC设计模式在客户端,相信我。这将为你节省大量的代码,更不用说更干净清晰的代码,更容易维护的代码。 一开始可能有点棘手,但相信我,这是一个很棒的图书馆。

其他回答

这是一个有趣的演示:

Backbone.js的介绍

提示(来自幻灯片):

浏览器中的Rails ?不。 JavaScript的MVC框架?有几分。 一个巨大的状态机?是的!

涉及大量用户交互和许多AJAX请求的web应用程序,需要不时更改,并实时运行(如Facebook或StackOverflow)应该使用MVC框架,如Backbone.js。这是构建好代码的最好方法。

如果应用程序很小,那么Backbone.js就太过分了,尤其是对第一次使用它的用户。

Backbone为您提供了客户端MVC,以及由此隐含的所有优势。

Backbone由Jeremy Ashkenas创建,他还编写了CoffeeScript。作为一个javascript较多的应用程序,我们现在所知道的Backbone负责将应用程序构建成一个一致的代码库。backbone的唯一依赖项Underscore.js也是DocumentCloud应用的一部分。

Backbone帮助开发人员在客户端web应用程序中管理数据模型,与传统服务器端应用程序逻辑中的规则和结构一样多。

使用Backbone.js的其他好处

将Backbone视为一个库,而不是一个框架 Javascript现在以一种结构化的方式组织起来,即(MVVM)模型 庞大的用户群体

I have to admit that all the "advantages" of MVC have never made my work easier, faster, or better. It just makes the whole codeing experience more abstract and time consuming. Maintenance is a nightmare when trying to debug someone elses conception of what separation means. Don't know how many of you people have ever tried to update a FLEX site that used Cairngorm as the MVC model but what should take 30 seconds to update can often take over 2 hours (hunting/tracing/debugging just to find a single event). MVC was and still is, for me, an "advantage" that you can stuff.

如果你打算在浏览器中构建复杂的用户界面,那么你可能会发现自己最终发明了构成框架的大部分部件,比如Backbone.js和Sammy.js。所以问题是,你是否在浏览器中构建了一些足够复杂的东西来值得使用它(这样你就不会自己发明同样的东西)。

If what you plan to build is something where the UI regularly changes how it displays but does not go to the server to get entire new pages then you probably need something like Backbone.js or Sammy.js. The cardinal example of something like that is Google's GMail. If you've ever used it you'll notice that it downloads one big chunk of HTML, CSS, and JavaScript when you first log in and then after that everything happens in the background. It can move between reading an email and processing the inbox and searching and back through all of them again without ever asking for a whole new page to be rendered.

It's that kind of app that these frameworks excel at making easier to develop. Without them you'll either end up glomming together a diverse set of individual libraries to get parts of the functionality (for example, jQuery BBQ for history management, Events.js for events, etc.) or you'll end up building everything yourself and having to maintain and test everything yourself as well. Contrast that with something like Backbone.js that has thousands of people watching it on Github, hundreds of forks where people may be working on it, and hundreds of questions already asked and answered here on Stack Overflow.

但是,如果您计划构建的东西不够复杂,不值得花费与框架相关的学习曲线,那么这一切都不重要。如果你仍然在构建PHP、Java或其他网站,而后端服务器仍然在根据用户的请求进行构建网页的所有繁重工作,而JavaScript/jQuery只是在这个过程中锦上添花,那么你将不需要或还没有准备好使用Backbone.js。