如何将backbone.js 与命名空间一起使用?
在过去一个小时左右的时间里,我一直在尝试让命名空间与backbone.js 一起使用。
我尝试了所有方法。问题是:
Backbone.Controller 希望通过构造函数(“新关键字”)进行初始化,否则 Backbone.history 将不会被设置。这是我试图放入命名空间的代码,例如“Site.Controllers”
var MainController = Backbone.Controller.extend({
routes: {
"help": "help", // #help
},
help: function(){}
});
var ws = new MainController
每当我尝试将 MainController 放入某个命名空间时,backbone.js 都会抱怨 MainController 不是构造函数 - 当然它是构造函数,因为似乎没有任何方法可以使用构造函数创建命名空间“树”。如果你们愿意,我可以列出我尝试过的所有方法,但它与上面提供的链接完全相同。我没有尝试将其放入闭包中,因为这会非常慢。
I have been trying to get namespaces to work with backbone.js for the last hour or so.
I have read: How do I declare a namespace in JavaScript?
And I tried all approaches. Here is the problem:
Backbone.Controller wants to be initialized through a constructur ("new keyword"), because otherwise Backbone.history won't be set. This is the code that I'm trying to put into a namespace, for example "Site.Controllers"
var MainController = Backbone.Controller.extend({
routes: {
"help": "help", // #help
},
help: function(){}
});
var ws = new MainController
Whenever I try to put the MainController into some namespace, backbone.js complains that MainController is not a constructor - of course it does, because there doesn't seem to be any way to make a namespace "tree" with constructor functions. If you guys want, I can list all the approaches I tried, but it's exactly the same as from the link provided above. I didn't try putting it into closures, because that is suggested to be very slow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对你想要实现的目标感到困惑。创建命名空间的一种几乎不会失败的方法是:
另外,是的,闭包确实更慢。但我不会担心这个,除非你创建了数百万次闭包。
I'm confused as to what your trying to achieve. An almost fail proof method of creating a namespace is :
Also yes closures are indeed slower. But I would not worry about this unless your creating the closures millions of times.