Backbone JS 不是很像 jQuery 吗?

发布于 2024-10-19 21:09:52 字数 208 浏览 7 评论 0原文

Backbone JS 强烈建议您使用 jQuery。然而,它做的事情并不像 jQuery 那样。例如,jQuery 消除了 new 运算符的必要性,backbone 大量使用了它。

另一方面,我正在寻找一个更多地基于原型继承而不是经典继承的框架(新)。 jQuery 不属于这一类别,这只是我倾向于的一种架构风格。

是否有任何框架使用原型继承,或者是您自己的桥梁模式?

Backbone JS highly recommends you use jQuery. However, it doesn't do things very jQuery. For example, jQuery removes the necessity of the new operator, backbone makes heavy use of it.

On another note, I'm looking for a framework that is based more around prototypal inheritance than classical inheritance (new). jQuery doesn't fall under this category, this is just an architecture style I am leaning towards.

Are there any frameworks that use prototypal inheritance, or is it roll your own bridge pattern?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请别遗忘我 2024-10-26 21:09:53

Backbone 和 jQuery 解决不同的问题...Backbone 本质上为您提供了一个结构,以便制作 Javascript 重型应用程序...它为您提供了模型、集合、视图和控制器(尽管仅基于一天的使用,感觉对我来说,控制器用于路由,而视图有点像经典控制器)

Backbone 依赖于 jQuery(或 Zepto,如果您愿意的话)来帮助它执行 AJAX 请求等操作。

你是对的 - 它不太像 jQuery,但它为你提供了一些非常不同的东西...


更新

从版本 0.5.0 开始,骨干网已将控制器重命名为路由器,这应该会让事情变得有点对于来自 MS MVC / Rails 等的人来说更明显...

“我们利用 0.5.0 版本澄清了一些命名。控制器现在是路由器”

http://documentcloud.github.com/backbone/#Router

Backbone and jQuery solve different problems... Backbone essentially gives you a structure in order to make Javascript heavy apps... It gives you Models, Collections, Views and Controllers (although, based on only a day of playing with it, it feels to me like controllers are used for routing, and the views are kind of like a classic controller)

Backbone has a dependency on jQuery (or Zepto if you're that way inclined) to help it do things like AJAX requests.

You are correct - it is not very jQuery like, but it is providing you with something very different...


UPDATE

As of version 0.5.0 backbone have renamed controllers to routers, which should make things a little more obvious for folks coming from MS MVC / Rails etc...

"We've taken the opportunity to clarify some naming with the 0.5.0 release. Controller is now Router"

http://documentcloud.github.com/backbone/#Router

贪了杯 2024-10-26 21:09:53

我完全同意保罗提供的答案,并且还想重申一下,你的问题本质上是不明确的。

Jquery 高度以 dom 为中心,为您提供了在 DOM 上进行操作的优秀工具。无论是更改样式、将远程内容加载到文档的某些部分、响应浏览器事件……(几乎)所有内容的核心焦点都集中在 DOM 上。对于这种功能,您可以对文档内容进行操作,原型继承以及更重要的是通过 DOM 访问小部件的样式(查看 JQueryUI 的 API)效果相当好。如果您将小部件识别为 javascript 对象,那么您还必须跟踪这些对象...如果您采用 JQueryUI 等编程风格,则不必这样做,因为您可以访问存在于通过浏览 DOM 结构或简单地通过其 id(本质上充当元素的全局标识符)来浏览 DOM。

backbone.js 完全是为了不同的目的而构建的。引言中明确指出,它是建立在以下基本理念之上的:将数据绑定到 DOM 是不好的。
当您构建按照backbone.js 约定构建的应用程序时,您本质上总是专注于可能以某种方式链接到DOM 的javascript 对象。
您正在定义与服务器数据源交互的模型、在操作数据内容时触发事件的模型、帮助您管理大型数据集的集合……无论如何,您总是在未硬连线到文档结构中的 JavaScript 对象上进行操作。对于这种场景,更常见的是根据传统的面向对象模型来思考。
一旦你有了一个对象,工作流程与你习惯使用 jQuery 的工作流程没有太大区别,因为骨干也像 jQuery 一样提倡观察者模式。
因此,与使用 JQuery 将事件处理程序绑定到 DOM 元素的方式相同,您可以将事件处理程序附加到由模型、收集器等分派的自定义事件。因此,两者可以很好地合并。

就其他框架而言,您可能需要查看 Knockout,它提供数据绑定和可观察值等,并且不需要您使用 new 关键字来创建实例,而是通过调用 ko 命名空间中的函数来创建实例,这可能会吸引您口味。 KO 拥有丰富的文档和代码示例,您可以探索它们来决定它是否适合您的口味。我无法对 KO 进行更多评论,因为我对它的了解有限,但就backbone.js而言,我强烈建议您不要仅仅因为您不喜欢某些东西的实现方式而放弃该框架。它优雅而稳健地完成了它应该做的事情,并且占用的空间非常小。

I fully agree with the answer provided by Paul and also would like to restate that your question is ambiguous in its essentials.

Jquery is highly dom-centric and provides you excellent facilities for operating upon the DOM. Be it changing styles, loading remote content to some portion of document, responding to browser events ... in (almost) everything the core focus is upon the DOM. For this kind of functionality, where you are operating upon document content, prototypal inheritance and more significantly the style of accessing widgets through the DOM (check out the APIs of JQueryUI) works rather well. If you identify widgets as javascript objects, then you have to keep track of the objects as well ... which you in case of the programming style followed by JQueryUI etc., you dont have to, because you can access any widget present in the DOM by navigating through the DOM structure or simply through its id (which essentially acts as a global identifier for the element).

backbone.js is altogether built for a different purpose. The very introduction clearly states that it is built upon the underlying philosophy that tying your data to the DOM is bad.
When you build an application that structured as per backbone.js conventions, you are essentially always concentrated on javascript objects which may be somehow linked to the DOM.
You are defining models that interface with server data sources, models which trigger events when the data contents are manipulated, collections which help you manage large data sets ... whatever, you are always operating on javascript objects which are not hardwired into the Document structure. For such scenario, it is more usual to think in terms of traditional object oriented model.
Once you have an object, the workflow is not much different from what you are habituated with using jQuery because backbone too, just like jQuery, advocates for the observer pattern.
So, in the same manner that you can bind event handlers to DOM elements with JQuery, you attach event handlers to custom events dispatched by models, collectors etc. So the two amalgamate well.

As far as other frameworks are concerned, you might want to checkout Knockout which provides data bindings and observables etc. and does not require you to use new keyword to create instances, rather instances are created by calling functions in ko namespace which might appeal to your tastes. KO has extensive documentation and code examples, which you can explore to decide whether it suites your tastes. I can not comment more on KO because I have limited knowledge about it, but as far as backbone.js is concerned I would very strongly recommend you not to dismiss the framework just because you dont like the way some things are implemented. It does elegantly and robustly what it is supposed to do and maintains an incredibly small footprint.

锦上情书 2024-10-26 21:09:53

我认为您对原型继承以及您在框架中真正想要的东西存在误解。如果您修改原型但从未使用 new 关键字,那么您就永远不会创建新对象。如果您正在寻找一个框架来抽象对象创建,那是另一个主题,尽管您不应该害怕使用 new 关键字;它是 JavaScript 的重要组成部分。

实际上,您可以在 jQuery 中使用 new 语法,当您不使用它时,jQuery 实际上会使用 new 语法以及您传递给的参数再次调用自己它。这是纯粹的语法糖,对任何事情都没有什么影响。

“另一方面,我正在寻找一个更多基于原型继承而不是经典继承的框架(新)。”

Backbone 通过内置于所有模型、集合和视图中的方法 extend 构建在原型继承之上。它完全没有问题,并且更容易使用,并且添加了诸如 super 之类的东西 - 一个到父级原型的挂钩 - 一些通过简单的 JS 原型设计可能更难做到的东西。

I think you suffer from a misunderstanding of prototypal inheritance and what you actually want in a framework. If you're modifying prototypes but never using the new keyword, then you're never creating new objects. If you're looking for a framework to abstract away object creation, that's another topic, though you shouldn't be afraid to use the new keyword; It's a big part of JavaScript.

You can actually use the new syntax with jQuery, and when you don't use it, jQuery is actually recalling itself again with the new syntax along with the arguments you passed to it. This is pure syntactic sugar and makes very little difference about anything.

"On another note, I'm looking for a framework that is based more around prototypal inheritance than classical inheritance (new)."

Backbone is built on prototypal inheritance through the method extend that is built into all models, collections, and views. It's quite problem free and easier to use and adds things like super - a hook into the parent's prototype - something that can be more difficult to do with just plain JS prototyping.

吻风 2024-10-26 21:09:53

Backbone.js 为 ajax 驱动的 Web 应用程序带来了编码标准,这些应用程序无需刷新页面即可提供数据。

jQuery 带来了一组工具来帮助您完成基本的事情,而无需担心运行代码的浏览器。

它们没有任何共同点。这就像将锤子 (jQuery) 与工具箱 (backbone.js) 进行比较。锤子只是工具箱的一部分,而不是相反。

所以,是的! Backbone.js 与 jQuery 不同。

Backbone.js brings a codding standard for ajax driven web-apps that don't have to refresh the page in order to serve data.

jQuery brings a set of tools to help you get basic things done without worrying about the browser you're running the code in.

They have nothing in common. It's like comparing a hammer (jQuery) with a toolbox (backbone.js). The hammer is just a part of the toolbox, not the other way around.

So, yes! Backbone.js is not like jQuery.

记忆之渊 2024-10-26 21:09:53

“我正在寻找一个更多基于原型的框架
继承比经典继承“new 运算符”。

。Javascript 上没有“经典”继承。实际上,除了使用“new”之外,没有其他标准的原型方法,也许 jQuery 只是提供了一个在内部执行“new”的方法。

var jQuery = function( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context, rootjQuery );
},

"I'm looking for a framework that is based more around prototypal
inheritance than classical inheritance the new operator".

There is no 'classical' inheritance on Javascript. Actually there is no other standard way to prototype than using 'new' maybe jQuery just provides a method who does the 'new' inside.

var jQuery = function( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context, rootjQuery );
},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文