Backbone.js的是否有jQuery的硬依赖? [英] Does Backbone.js have a hard dependency on jQuery?

查看:115
本文介绍了Backbone.js的是否有jQuery的硬依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据骨干JS网站:

主干的只能硬依赖是Underscore.js(> 1.3.1)。对于REST风格的持久性,历史支持通过Backbone.Router和DOM操作与Backbone.View,包括json2.js,无论是与jQuery的(1.4.2)或的Zepto。

Backbone's only hard dependency is Underscore.js ( > 1.3.1). For RESTful persistence, history support via Backbone.Router and DOM manipulation with Backbone.View, include json2.js, and either jQuery (1.4.2) or Zepto.

我code测试下方,去除jQuery和骨干视图抛出一个错误。

I tested with code below, removing jQuery and the Backbone view throws an error.

<html>
<head>
    <title>asda</title>


    <!--<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>-->
    <script src="http://documentcloud.github.com/underscore/underscore.js"></script>
    <script src="http://documentcloud.github.com/backbone/backbone.js"></script>            
    <script type="text/javascript" charset="utf-8" async defer>
        SearchView = Backbone.View.extend({
            initialize: function(){
                alert("Alerts suck.");
            }
        });

        // The initialize function is always called when instantiating a Backbone View.
        // Consider it the constructor of the class.
        var search_view = new SearchView;
    </script>   
</head>
<body>

</body>
</html>

哪有没有jQuery的Backbone.View和Backbone.Router工作?

How can Backbone.View and Backbone.Router work without jQuery?

推荐答案

虽然Backbone.View不依赖于的的DOM操作库,即你不能使用JavaScript未在有组织的香草某种类型的库中,可以配置骨干用自己喜欢的任意库。

While Backbone.View does depend on a DOM manipulation library, ie you can't use vanilla javascript that is not organized in to a library of some sort, you can configure Backbone to use whatever library you'd like.

请参阅从骨干源如下:

// Set the JavaScript library that will be used for DOM manipulation and
// Ajax calls (a.k.a. the `$` variable). By default Backbone will use: jQuery,
// Zepto, or Ender; but the `setDomLibrary()` method lets you inject an
// alternate JavaScript library (or a mock library for testing your views
// outside of a browser).
Backbone.setDomLibrary = function(lib) {
  $ = lib;
};

调用此方法将允许你使用任何你想要的库。

Calling this method will allow you to use whatever library you want.

例如:

Backbone.setDomLibrary(myCustomLibrary);

这篇关于Backbone.js的是否有jQuery的硬依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆