在另一个文件中声明骨干扩展类 - CoffeeScript
我有以下扩展 Backbone.View 的类,我希望所有主干视图都从该类继承:
class BaseView
constructor: (options) ->
@bindings = []
Backbone.View.apply(@, [options])
_.extend(BaseView.prototype, Backbone.View.prototype, {
#etc. tec.
BaseView.extend = Backbone.View.extend
然后我可以像这样扩展我自己的视图:
class BusinessUnitsView extends BaseView
initialize: (options) ->
如果它们位于同一个文件中,那么这一切都可以正常工作,但是如果我将 BaseView 分成另一个文件,我收到一条错误消息:
BaseView 未定义
如何将 BaseView 保存在不同的文件中并使用它来扩展我的自定义视图?
I have the following class that extends Backbone.View, I want all my backbone views to inherit from this class:
class BaseView
constructor: (options) ->
@bindings = []
Backbone.View.apply(@, [options])
_.extend(BaseView.prototype, Backbone.View.prototype, {
#etc. tec.
BaseView.extend = Backbone.View.extend
I can then extend my own view like this:
class BusinessUnitsView extends BaseView
initialize: (options) ->
This all works fine if they are in the same file but if I separate BaseView into a different file, I get an error message:
BaseView is undefined
How can I keep the BaseView in a different file and use it to extend my custom views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其放在
BaseView.extend = Backbone.View.extend
下,它使您的 BaseView 全局可访问
我总是这样声明我的类,效果很好
Put this under
BaseView.extend = Backbone.View.extend
it makes your BaseView global accessible
I always declare my classes like this and it works great