【发布时间】:2016-05-13 17:49:39
【问题描述】:
我在灯箱内有backbonejs form,html <select> 作为子视图。
对于我从服务器加载的select<option> 数据,我对此select 有单独的模型和集合。
<select name="organization" id="organization" class="main__form--select main__form--select--js">
<option value="no">Organizations not found, Please add one</option>
</select>
选项模型(optionModel)
return Backbone.Model.extend({
defaults : {
"name" : 'KFC',
"email" : 'info@kfc.com',
"image" : '/kfc.jpg',
"descrption" : 'Lorem Ipsum'
}
});
这是模型的视图
return Backbone.View.extend({
model : optionModel,
template : _.template(template),
render : function () {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
这是选项集合
return Backbone.Collection.extend({
model : optionModel,
getQuery : function(){
//all my query codes
}
});
选项集合视图render()代码
this.collection.each(function (optionModel) {
// inserting each options view to an array
_this._optionsViewArray.push(
new OptionView({
model: optionModel
}).render().el
);
});
//inserting array to collection view container
_this.$el.html(_this._optionsViewArray);
return this;
我的父视图(表单视图)我在渲染函数后创建,带有下划线 _.wrap 并在该函数内部
//<select>
var _selector = this.$el.find('#organization');
optionsView = new OptionsCollectionView({
collection : optionsCollection,
$el: _selector
});
optionsCollection.getQuery();
optionsView.render();
但表单加载完美,选项集合查询成功,但<select> html 上没有任何变化,它没有更新。
【问题讨论】:
-
什么是
_this..?什么是_optionsViewArray..?如果它是一个 javascript 数组,谁告诉你 jQueryhtml方法接受一个数组..?请提供minimal reproducible example
标签: backbone.js