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

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


当前回答

它还添加了使用KVO控制器和视图的路由。您将能够使用它开发“AJAXy”应用程序。

可以把它看作是一个轻量级的Sproutcore或Cappuccino框架。

其他回答

js是一个帮助你组织代码的JavaScript框架。它实际上是构建应用程序的基础。它不提供小部件(如jQuery UI或Dojo)。

它为您提供了一组很酷的基类,您可以扩展这些基类来创建与服务器上RESTful端点接口的干净JavaScript代码。

它还添加了使用KVO控制器和视图的路由。您将能够使用它开发“AJAXy”应用程序。

可以把它看作是一个轻量级的Sproutcore或Cappuccino框架。

如果你打算在浏览器中构建复杂的用户界面,那么你可能会发现自己最终发明了构成框架的大部分部件,比如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。

这是一个很好的入门视频: http://vimeo.com/22685608

如果你想了解更多关于Rails和Backbone的知识,Thoughtbot有这本非常好的书(不是免费的): https://workshops.thoughtbot.com/backbone-js-on-rails

这是我在BackboneJS上写的一篇快速入门文章。希望能有所帮助! http://www.infragistics.com/community/blogs/nanil/archive/2013/04/01/exploring-javascript-mv-frameworks-part-1-hello-backbonejs.aspx