2013-09-30 134 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() 
相關問題