我覺得你最好把手風琴留在一個視圖上,然後在每個面板裏面都有獨立的視圖。畢竟,<h2>
是整個手風琴的控制器,而不是特定的面板。
你最好有一些每個小組的觀點是這樣的:
var P = Backbone.View.extend({
render: function() {
// Add the panel's content to this.$el (which is a <div> by default).
return this;
}
});
然後手風琴看法是這樣的:
var A = Backbone.View.extend({
render: function() {
var panels = [ ... ];
for(var p, i = 0; i < panels.length; ++i) {
p = new P({ ... });
this.$el.append('<h3><a>' + panels[i] + '</a></h3>');
this.$el.append(p.render().el);
}
// The accordion wants to know the sizes of things so
// we let the DOM sort itself out before binding the
// accordion.
var _this = this;
setTimeout(function() { _this.$el.accordion() }, 0);
return this;
}
});
那麼你可以簡單$('#something').append((new A).render().el)
,它均能同時進行工作留下應有的一切。
你也可以添加一個title
方法將P
意見,然後A
可以要求面板就是它的名字/標題/頭應該是讓所有的每個面板的信息很好地包含在每個面板視圖。
演示:http://jsfiddle.net/ambiguous/Y49W8/
您可以使用$(「選擇」),而不是指定一個標籤名選擇元素。 – McGarnagle