我正在使用Backbone.js v0.5.5处理单页应用程序,并且在应用程序的一个“页面”上有一个Master View(audioOnDemand)和两个辅助视图(audioPlay和audioNav)。我希望辅助视图是可路由的,以便用户可以作为示例转到'#audio_ondemand','#audio_ondemand/id /:id'或'#audio_ondemand/page /:page',并且所有内容都会加载。为了做到这一点,我最终不得不在我的控制器/路由器中放置一些jQuery代码,这感觉很不方便。这就是为什么我在这里,看看有没有更好的方法。Refactor Me:Backbone.js子视图渲染优化
这是我的控制器/路由器。
audio_ondemand: function(splat) {
this._audioondemandview = new AudioOnDemandView();
this._audioondemandview.render();
if (splat) {
var id_patt = /id\/(\d*)/i;
var id = id_patt.exec(splat);
id = (id) ? id[1] : null;
//console.log("id is: "+id);
var page_patt = /^page\/(\d*)/i;
var page = page_patt.exec(splat);
}
page = (page) ? page[1] : 1;
//console.log("page is: "+page);
if (id) {
setTimeout('app.play_audio("'+id+'");', 500);
}
setTimeout('app.audio_nav("'+page+'");', 500);
},
audio_nav: function(page) {
var nav_holder = $('#pagination-nav');
if (nav_holder.length == 0) {
app.audio_ondemand('page/'+page);
} else {
var audios = this._audios;
var nav = new AudioNavView({el: nav_holder, audios: audios, page: page});
nav.render();
}
},
play_audio: function(id) {
var audio_holder = $('#audio-player');
if (audio_holder.length == 0) {
app.audio_ondemand('id/'+id);
} else {
var audio = this._audios.getByUid(id);
this._audioview = new AudioView({el: $('#audio-player'), model: audio});
this._audioview.render();
}
}
的观点并不做什么特别突出的,但这里是我的“主人”模板(使用jQuery模板):
<script id="audioOnDemandTmpl" type="text/x-jquery-tmpl">
<div class="sub-header">
<strong>Audio</strong> On Demand
</div>
<div id="currently-playing">
<div id="currently-header">Currently Playing</div>
<div id="current-holder"></div>
</div>
<div id="audio-player"></div>
<div id="pagination-nav"></div>
<div id="audios-holder"></div>
</script>
我想现在你得到的困境承担。主视图必须在辅助视图之前呈现,否则辅助视图不具有要附加的适当DOM元素。但是,如果我希望次要视图可路由,则必须执行'hacky'jQuery检查以查看主视图是否已呈现。
感谢您的任何想法或建议,并让我知道如果您需要更多的细节!
UPDATE:因为它可能有助于更好地给出这个想法,下面是导航视图和模板。
<script id="audioNavTmpl" type="text/x-jquery-tmpl">
{{if prev > 0}}<a href="#audio_ondemand/page/${prev}">Prev</a> | {{/if}}${current}{{if next}} | <a href="#audio_ondemand/page/${next}">Next</a>{{/if}}
</script>
<script id="audioTmpl" type="text/x-jquery-tmpl">
<div class="audio-holder">
<div class="title-author">
<div class="title"><a href="#audio_ondemand/id/${attributes.uid}">${attributes.title}</a></div>
<div class="author">${attributes.author}</div>
</div>
<div class="topic-date">
<div class="topic"><strong>${attributes.topic}</strong></div>
<div class="date">${attributes.date}</div>
</div>
</div>
</script>
var AudioNavView = Backbone.View.extend({
audioNavTemplate: $('#audioNavTmpl').template(),
audioTemplate: $('#audioTmpl').template(),
initialize: function(options) {
this.audios = options.audios.models;
var temp_page = parseInt(options.page);
this.page = [
{
prev: temp_page - 1,
current: temp_page,
next: temp_page + 1
}
];
this.slice_start = (temp_page - 1) * 11;
this.slice_end = temp_page * 11;
this.audios = this.audios.slice(this.slice_start, this.slice_end);
if (this.audios.length <= 11) {
this.page[0]["next"] = false;
}
},
render: function() {
//console.log(this.page);
this.el.empty();
var sg = this;
$('#audios-holder').fadeOut('fast', function() {
$.tmpl(sg.audioNavTemplate, sg.page).appendTo(sg.el);
$(this).empty();
$.tmpl(sg.audioTemplate, sg.audios).appendTo($(this));
$(this).fadeIn('fast');
});
return this;
}
});
我应该如何更好地设置它,以便如果有人从'#home'直接跳到'#audio_ondemand/page/2',路由器会确保在audioNavView之前加载audioOnDemandView?
“主”视图和“次要”视图之间的区别对我而言并不清楚。它们都看起来像可路由的意见。在我看来你的路由器应该有一个跟踪当前呈现的所有视图(路由本身是一个理想的关键),并路由显示或构建和显示视图。 –
为导航视图添加了更多代码示例,以更好地了解发生了什么。 – Andrew565