1

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?

user229044
  • 222,134
  • 40
  • 319
  • 330
dagda1
  • 23,659
  • 54
  • 216
  • 399
  • CoffeeSript's auto-scoping is a very common source of confusion along these lines. This question, and the answers on it, should clarify matters: http://stackoverflow.com/questions/5211638/pattern-for-coffeescript-modules/ – Trevor Burnham Nov 10 '11 at 16:02

1 Answers1

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
Michael Koper
  • 9,192
  • 7
  • 42
  • 57
  • So it works now right? Or you need a better answer? I updated my answer the way I always declare my backbone classes. Hope it works! – Michael Koper Nov 10 '11 at 15:14