将 Backbone.js 与 _.noConflict() 一起使用

发布于 2024-12-01 14:25:27 字数 118 浏览 1 评论 0原文

我希望将 Backbone.js 与命名空间下划线库一起使用。有谁知道我如何告诉 Backbone 引用 下划线 _

谢谢! 马特

I'm looking to use Backbone.js with a namespaced underscore library. Does anyone know how I can tell Backbone to refer to say, underscore and not _

Thanks!
Matt

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

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

发布评论

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

评论(1

南城旧梦 2024-12-08 14:25:27

截至今天(版本 0.5.3),Backbone 本身还没有准备好,但可以做到:

您需要放置请求 underscore.jsbackbone.js 的脚本标记script 标记中的第一个/早期,并在 underscore,backbone 脚本之间的脚本中执行 _.noConflict()和其余的脚本加载。这是一个示意图版本:(

<!DOCTYPE html>
<html>
  <head>
    <title>Labbo</title>
    <script src="underscore.js"></script>
    <script src="backbone.js"></script>
    <script>
      var underscore = _.noConflict();
    </script>

    <script>
      // In it's own script tag for readability
      console.log('_ object: ', _);
      console.log('"underscore" object: ', underscore);
      var m = new Backbone.Model({});
      console.log('Dummy backbone model: ', m);
    </script>

    <!-- Load your other scripts. From here on the '_' global isn't defined -->
    <!-- any more. -->
    <!-- <script src="your_other_scripts.js"></script> -->
  </head>

  <body>
    Open Developer Tools / Firebug and check the output in the console.
  </body>
</html>

无法将其放在 jsfiddle 上,因为为了演示,您需要控制脚本标签的确切位置)。

As of today (version 0.5.3) Backbone isn't ready for this in it self but it can be done:

You need to put your script tags requesting underscore.js and backbone.js first/early among your script tags, and do your _.noConflict() in a script between the underscore,backbone scripts and the rest of the script loading. Here's a schematic version:

<!DOCTYPE html>
<html>
  <head>
    <title>Labbo</title>
    <script src="underscore.js"></script>
    <script src="backbone.js"></script>
    <script>
      var underscore = _.noConflict();
    </script>

    <script>
      // In it's own script tag for readability
      console.log('_ object: ', _);
      console.log('"underscore" object: ', underscore);
      var m = new Backbone.Model({});
      console.log('Dummy backbone model: ', m);
    </script>

    <!-- Load your other scripts. From here on the '_' global isn't defined -->
    <!-- any more. -->
    <!-- <script src="your_other_scripts.js"></script> -->
  </head>

  <body>
    Open Developer Tools / Firebug and check the output in the console.
  </body>
</html>

(Couldn't put this on jsfiddle beacuse to demo you need control over excactly where the script tags go).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文