Backbone.jsアプリケーションで複数選択のjQueryプラグインを動作させるための魔法のトリックは何ですか? SumoSelect、Bootstrap-Multiselect、Chosen、および jQuery UI の MultiSelect を使用しようとしました。
モデルがあり、それはコレクションです。以下のコードを使用して、View の render メソッドで SumoSelect プラグインなどを設定しています。
render : function() {
....
// Time for DOM
setTimeout(function(){
$(".dropdown-features", this.$el).SumoSelect()
}, 100);
return this;
},
クラスdropdown-featuresは、このようにテンプレートファイルで定義されています
<div class="filter-header">
<strong>${title}</strong>
</div>
<div>
<select class="dropdown-features">
<option value="">Choose...</option>
</select>
</div>
</div>
Model と Collection は Backbone.js アプリケーションのように通常定義されます。
MyApp.Models = MyApp.Models || {};
(function () {
'use strict';
MyApp.Models.Facet = Backbone.Model.extend({
idAttribute: "name"
});
})();
と
MyApp.Collections = MyApp.Collections || {};
(function () {
'use strict';
MyApp.Collections.Facets = Backbone.Collection.extend({
model: MyApp.Models.Facet,
initialize : function(models, options) {
if (_.isUndefined(options) ||
_.isUndefined(options.label) ||
_.isUndefined(options.title)) {
throw new Error('Label and title must be given Facets collection in initialization.');
} else {
this.label = options.label;
this.title = options.title;
}
},
getName: function() {
return this.title;
},
getLabel: function() {
return this.label;
},
getFirstLabel: function() {
return this.first().get('name');
}
});
})();
純粋な HTML 選択タグを使用するとすべて正常に動作しますが、上記のプラグインを使用しようとすると、選択オプションにデータを取得できません。選択ウィジェットは新しいスタイルに変更されますが、「Choose...」以外は何も含まれていません。
私はかなり新しい Backbone.js ユーザーで、Backbone.js アプリケーションで UI プラグインを使用したことがありません。
ありがとう、
マイク