2013-09-30 45 views
0

我有一个模型下面如何从模型系列动态填充选择选项

class Colors extends Backbone.Model 
    name: -> [@get("name")] 
    value: -> [@get("value")] 

集合定义如下

class @ColorsCollection extends Backbone.Collection 
    model: Colors 

选择标签的定义界定如下

%select{name: "colorslist" type: "hidden" value: "" } 

在一个事件,我想动态地使用从ColorsCollection中获取的数据填充颜色列表选择选项。

我一直在寻找select2文档,但无法找到任何相关示例。

回答

0

基本上,您将绑定到重置事件并替换html并启动select2插件。

我知道这个插件有一些内部的方法 - 但为什么要处理必须通过文档梳理。

class View extends Backbone.View 

    initialize: -> 
    @collection = new ColorsCollection 
    # Bind to reset event 
    @listenTo @collection, "reset", @updateSelect 
    @collection.fetch() 

    updateSelect: (collection) -> 
    # Use template engine (eg. Handlebars) to redraw the html 
    @$el.find('#selection').html tmpl(@collection) 
    # Start select2 
    @$el.find('#selection > select').select2() 
相关问题