I have a working coffeescript/backbone idiom that looks like this:
SidebarWidgets = ((() ->
SidebarWidgets = { }
class SidebarWidgetPrototype extends Backbone.View
initialize: (options) ->
@template = $(options.templateId).html()
render: () ->
$(@el).html(_.template(@template, @model.toJSON()))
@el
class SidebarWidgets.user extends SidebarWidgetPrototype
class SidebarWidgets.shoppingcart extends SidebarWidgetPrototype
class SidebarWidgets.messages extends SidebarWidgetPrototype
SidebarWidgets
)())
class Sidebar extends Backbone.View
views: ['user', 'shoppingcart', 'messages']
initialize: (options) ->
@subviews = { }
_.each(@views,(v) =>
subviews[v] = news SidebarWidgets[v](
model: cxDatasets[v]
id: 'sidebar-' + v
templateId: '#sidebar-' + v + 'template'
)
)
render: () ->
$(@el).html()
_.each(@views, (v) =>
$(@el).append(@subview(v).render())
)
The intent of this idiom is to provide a list of backbone views that the sidebar view will then incorporate, while providing the opportunity (but not the necessity) to override or enhance one or more methods of a widget.
The thing that irks me is that, for those views that do not need modification, they still need to be named explicitly by the class syntax of Coffeescript.
Is there a way to create an anonymous class with the Coffeescript syntax? Can you say something like (the following is pseudocode):
thisclass = extend BackboneView
initialize: (options) ->
If so, how?
thisclass = class extends BackboneView
initialize: (options) ->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With