开发者

How do you do anonymous classes in Coffeescript?

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: cxDa开发者_如何学Pythontasets[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) ->
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜