Fran b
Fran b

Reputation: 3036

Extend an hash attribute and prevent overwrite it with coffeescript and marionettejs

I have a view like this:

class MyView1 extends Backbone.Marionette.Layout
  template: JST['templates/view1']

  regions:
     region1: '.region1'

  ## Here some methods....

And now I want extend this class for add some regions and methods

class MyView2 extends MyView1
  template: JST['templates/view2']

  regions:
    region2: '.region2'

This overwrites template and regions attributes. But I want add the region2 to hash of regions, not overwrite it. So regions would be a hash with region1 and region2.

How can I get it?

Upvotes: 0

Views: 122

Answers (1)

arghbleargh
arghbleargh

Reputation: 3170

Here's a workaround:

class MyView1 extends Backbone.Marionette.Layout
  template: JST['templates/view1']

  regions:
    region1: '.region1'

class MyView2 extends MyView1
  template: JST['templates/view2']

  MyView2::regions = {}
  for k, v of MyView1::regions
    MyView2::regions[k] = v
  MyView2::regions.region2 = '.region2'

But maybe the following would be cleaner and serve your purposes equally well:

class MyView1 extends Backbone.Marionette.Layout
  constructor: ->
    @regions =
      region1: '.region1'

  template: JST['templates/view1']

class MyView2 extends MyView1
  constructor: ->
    super()
    @regions.region2 = '.region2'

  template: JST['templates/view2']

Edited to use more idiomatic coffeescript :: instead of .prototype, as suggested by comment

Upvotes: 1

Related Questions