骨干应用组织

发布于 2024-12-17 19:44:16 字数 1079 浏览 1 评论 0原文

我按照本页所述组织了我的 Backbone 应用程序 http://weblog .bocoup.com/organizing-your-backbone-js-application-with-modules

对于那些还没有读过文章的人,我将简要解释一下这个想法: 我定义了一个变量来保存我的所有模块。

var Application = {
  module: function(){
    var modules = {};
    return function(name){
      if (typeof modules[name] == 'undefined')
      {
        modules[name] = {
          Model: {},
          Collection: {},
          Views: {},
        }
      }
      return modules[name];
    };
  }()
};

Application.module 是一个将按名称返回模块的函数。然后我将我的应用程序模块定义如下:

(function(chat){
  chat.Model = Backbone.Model.extend({ ... }),
  chat.Collection = Backbone.Collection.extend({ ... }),
  etc.
})(Application.module('Chat'));

一切似乎都很好 - 所有代码都分为模块,但是当我尝试从模块的类之一实例化变量时。

$(document).ready(function(){
  var foo = new Application.module('Chat').Collection();
}); 

上面的代码给了我 Backbone 的“未捕获类型错误:无法读取未定义的属性‘绑定’”错误,我不明白为什么?

I organized my Backbone app as described at this page http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules

For those who hasn't read an article I'll explain the idea briefly:
I define single variable that will hold all of my modules.

var Application = {
  module: function(){
    var modules = {};
    return function(name){
      if (typeof modules[name] == 'undefined')
      {
        modules[name] = {
          Model: {},
          Collection: {},
          Views: {},
        }
      }
      return modules[name];
    };
  }()
};

Application.module is a function that will return module by name. Then I define my application modules as following:

(function(chat){
  chat.Model = Backbone.Model.extend({ ... }),
  chat.Collection = Backbone.Collection.extend({ ... }),
  etc.
})(Application.module('Chat'));

Everything seems to be ok - all the code is separated into modules, but when I am trying to instantiate a variable from one of the module's classes.

$(document).ready(function(){
  var foo = new Application.module('Chat').Collection();
}); 

Code above gives me Backbone's "Uncaught TypeError: Cannot read property 'bind' of undefined" error and I cannot understand why?

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

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

发布评论

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

评论(2

豆芽 2024-12-24 19:44:16

我上周也看到了这个错误...你正确包含 underscore.js 吗?
就我而言,我在页面中包含了 underscore.js,但在backbone.js 引用之后。

underscore.js 确实必须先加载,然后backbone 才能完成它的事情:)

这样做:

<script src="underscore.min.js"></script>
<script src="backbone.min.js"></script>

i saw that error last week too... did you include underscore.js correctly?
in my case i had underscore.js included in the page, but after the backbone.js reference.

underscore.js really has to be loaded before backbone can do it's thing :)

do it like this:

<script src="underscore.min.js"></script>
<script src="backbone.min.js"></script>
别想她 2024-12-24 19:44:16

我强烈建议使用 [require][1]。
它非常容易实现,您可以为所有 .js 库指定依赖项。

如果你一旦无法探测它,就没有它就活不下去。

您可以在此处查看其工作原理的示例:
https://github.com/charnekin/api/blob/master/js /main.js

I recommend strongly the use of [require][1].
It's very easy to implement and you can especify dependencies for all your .js librarys.

If you probe it once you can't will live without it.

Here you can see an example how it works:
https://github.com/charnekin/api/blob/master/js/main.js

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