如何使用EmberJS扩展名称空间 [英] How to extend namespaces with EmberJS
问题描述
我已经在Javascript中编程了一段时间。最近我做了一个很大的jQuery项目,并应用了这个精彩文章中描述的模块模式: http://www.bnbatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
I've been programming in Javascript for a while. Recently I made quite a huge jQuery project and applied the Module Pattern as described in this wonderful article: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
这一切都罚款和花花公子,代码看起来光滑可控,但我觉得可以更好。我花了一天寻找一些Javascript框架,其中大部分是:
This all went fine and dandy and the code looks slick and manageable, but I felt it could be better. I've spend the day looking for some Javascript frameworks, mostly ones that:
- 有UI绑定支持
- 有一个模板系统
- 可以使用jQuery
- 帮助我按照与模块模式相似的方式组织我的代码
- Have UI binding support
- Have a templating system
- Can work with jQuery
- Help me organize my code in a similar way as I did with the module pattern
我偶然发现了像AngularJS,KnockOutJS,SpineJS,JavascriptMVC等框架。 - 是EmberJS。
I've stumbled across frameworks like AngularJS, KnockOutJS, SpineJS, JavascriptMVC, etc. The one that really sticked out - and was quite praised - was EmberJS.
我决定给它一个镜头,但这并不容易。 EmberJS教程的可用性非常有限。尝试了很久之后,我设法得到一些东西运行,我喜欢EmberJS做的!只有一件事我似乎无法想象 - 这也是我的问题:如何扩展Ember命名空间(使用Ember.Application.create制作)?
I decided to give it a shot, but it has not been easy. The availability of tutorials for EmberJS is very limited. After trying for a long time I managed to get some stuff running and I like what EmberJS does! There is only one thing I can't seem to figure out - which is also my question: How can I extend an Ember namespace (made with Ember.Application.create)?
要澄清:我的项目的旧版本有一个Core命名空间和一个Util命名空间。两者都包含其他类别可以使用的各自的功能。如何在初始命名空间之上使用具有函数的Core-and Util命名空间?
For clarification: The old version of my project had a Core-namespace and a Util-namespace. Both contained their respective functions which all other classes could use. How can I have a Core- and Util-namespace with functions on top of the initial-namespace?
我只是这样做:
MyNamespace = Ember.Application.create();
MyNamespace.Core = Ember.Application.create();
MyNamespace.Util = Ember.Application.create();
还有别的东西?
推荐答案
您无法嵌套 Ember.Namespace
的(其中 Ember.Application
正在 Ember.Namespace
的子类),请参阅#683 。
You can't nest Ember.Namespace
's (where Ember.Application
being a subclass of Ember.Namespace
), see issue #683.
Tome Dale是核心贡献者之一,为复杂应用程序的布局添加了一个有趣的答案,请参阅评论。
Tome Dale, one of the core contributors, added an interesting answer about the layout of complex applications, see the comment.
所以你可以使用 App = Ember.Application.create()
并在此命名空间下创建控制器,视图等。或者你可以 - 如果你打算在其他项目/应用程序中重用一些代码 - 拆分命名空间如下:
So you can either just use App = Ember.Application.create()
and create your controllers, views, etc under this namespace. Or you can - if you intend to reuse some code in other projects/applications - split the namespace's like this:
Util = Ember.Namespace.create({
importantNames: 'Einstein'.w()
});
Core = Ember.Namespace.create({
sqrt: function(x) { return Math.sqrt(x); }
});
App = Ember.Application.create();
...
App.myListController = Ember.Object.create({
importantNamesBinding: 'Core.importantNames',
sqrtBinding: 'Util.sqrt'
});
一个 Ember.Application
extends Ember.Namespace
,并添加处理事件的功能(点击,...)。当您在 Core
和 Util
命名空间中编写具有视图的应用程序时,这些东西很有用,您不需要东西,这就是为什么这是只是一个 Ember.Namespace
。
An Ember.Application
extends Ember.Namespace
, and adds functionality about handling events (click, ...). This stuff is useful when you write an application with views - in your Core
and Util
namespace you wouldn't need that stuff, that's why this is just an Ember.Namespace
.
这篇关于如何使用EmberJS扩展名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!