Reputation: 25
How can I call the super's render function in backbone (coffeescript)?
If not in coffeescript, I've heard
MyModel.__super__.render.call(this);
will work, but MyModel in this case is exports.MyModel, how do I use this function if its an element of exports?
Thanks in advance
Upvotes: 2
Views: 1277
Reputation: 20437
There are some drawbacks to the coffeescript class approach in a Backbone environment:
class SecondaryLevelClass extends TopLevelClass
syntax changes the traditional Backbone extension model, which could be confusing.It may be worth using the regular Backbone extend syntax with the tradeoff of calling super in a more verbose way, like this:
TopLevelClass Backbone.View.extend
initialize: -> @render()
render: ->
console.log 'Render TopLevelClass'
@
SecondaryLevelClass = TopLevelClass.extend
initialize: -> @render()
render: ->
SecondaryLevelClass.__super__.initialize.call(this)
console.log 'Render SecondaryLevelClass'
@
t = new TopLevelClass # el: $("#first_div")
s = new SecondaryLevelClass # el: $("#second_div")
Another option is a mixin like this: http://pivotallabs.com/a-convenient-super-method-for-backbone-js/
Upvotes: 1
Reputation: 4771
Since you're trying to call the super render method from inside the render method you could just something like this:
class TopLevelClass extends Backbone.View
initialize: ->
@render()
render: ->
console.log 'Render TopLevelClass'
@ # return this
class SecondaryLevelClass extends TopLevelClass
initialize: ->
@render()
render: ->
super()
console.log 'Render SecondaryLevelClass'
@ # return this
t = new TopLevelClass
# el: $("#first_div")
s = new SecondaryLevelClass
# el: $("#second_div")
Source: http://coffeescript.org/#classes
edit:
@lublushokolad is correct. The Backbone documentation recommends that render
returns this
Upvotes: 1