Declaring backbone extension class in another file

2019-08-13 03:50发布

问题:

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?

回答1:

Put this under BaseView.extend = Backbone.View.extend

@.BaseView = BaseView

it makes your BaseView global accessible

I always declare my classes like this and it works great

class BaseView extends Backbone.View

@.BaseView = BaseView