渲染jQuery对象我是相当新的Backbone.js的发展,同时为了显示一个子视图却碰上了一个有点路障。通过骨干查看
目前,我采取了多种观点来呈现的自定义下拉按钮,以及其他元素。我已经采取了基于DocumentCloud's code
这种方法这是我到目前为止有:
app.ui.SelectMenu = Backbone.View.extend({
className: 'btn-group group-item',
options: {
id: null,
standalone: false
},
events: {
"click .dropdown-menu a": "setLabel"
},
constructor: function (options) {
Backbone.View.call(this, options);
this.items = [];
this.content = JST['common-select_button'];
this.itemsContainer = $('.dropdown-menu', $(this.content.render()));
// Add any items that we may have added to the object params
if (options.items) {
this.addItems(options.items);
}
},
render: function() {
this.$el.html(this.content.render({
label: this.options.label,
items: this.itemsContainer
}));
this._label = this.$('.menu-label');
return this;
},
setLabel: function (label) {
$(this._label).text(label || this.options.label);
},
addItems: function (items) {
this.items = this.items.concat(items);
var elements = _(items).map(_.bind(function (item) {
var attrs = item.attrs || {};
_.extend(attrs, { 'class': 'menu_item' + (attrs['class'] || '') });
var el = this.make('li', attrs, item.title);
return el;
}, this));
$(this.itemsContainer).append(elements);
}
});
到目前为止,我已经成功地呈现我的按钮,以及相应的标签,但我似乎无法填充.dropdown-menu
调用addItems
功能时。
我假设当render
命中时,items变量不能填充,因为我传递了一个jQuery对象而不是一个字符串,然而无论何时我使用items: this.itemsContainer.html()
,只需粘贴由引号括起来的html。 ..我可以简单地更换引号,但只是感觉就像一个黑客给我。
任何帮助将不胜感激。谢谢!
哇,感谢伟大的答案!非常丰富和有益的。 我编辑了Github链接,以正确反映我正在工作的来源。我认为,因为这是[jashkanes](https://twitter.com/#!/jashkenas)的工作原理,那么使用'constructor'将会很好。再次感谢详细的回复,队友。 –
@Jaysche:我没有在那里使用'constructor',也许它只是早于Backbone中initialize'的旧代码。使用'$(this.el)'而不是'this。$ el'确实表明它有点老,并且可能没有为更新的Backbones进行更新。 –
我想这是有道理的。我正在读一些有关DocumentCloud的文章,显然Backbone是通过从项目中提取的部分代码创建的,所以这很可能是这里发生的。再次感谢! –