[英]Backbone: Render view that has been created before
我有包含導航欄和地圖的外殼視圖。 向該視圖渲染使用先前渲染的地圖的其他視圖。 當我轉到視圖perfil
,地圖已刪除,但導航欄仍保持到目前為止。 我的問題是,當回到家中時,地圖沒有出現,而是僅出現了包含地圖的div
。 波紋管顯示示例:
查看外殼並查看主頁:
去查看Perfil:
回到家:
這是我的代碼:
app.js
var ev = new Application();
ev.Router = Backbone.Router.extend({
routes: {
"": "home",
"evento/:id" : "evento",
"criarevento" : "criarevento",
"perfil" : "perfil"
},
home: function(){
setTimeout(function(){
$('#rightcolumn').html(new ev.views.Home(ev.shell.map).el);
}, 0);
},
... // other views
perfil: function(){
setTimeout(function(){
$('#home').html(new ev.views.Perfil(ev.shell.template).el);
}, 0);
}
});
$(document).on('ready', function() {
ev.user = new ev.models.Person(); // Holds the authenticated Facebook user
// Load HTML templates for the app
ev.templateLoader.load(['shell', 'home', 'search_keyword', 'evento', 'login', 'quemvai', 'criar_evento', 'home_criar_evento', 'perfil'], function () {
ev.shell = new ev.views.Shell({el: "#shell", model: ev.user});
ev.router = new ev.Router();
Backbone.history.start();
});
});
perfil.js
ev.views.Perfil = Backbone.View.extend({
initialize: function(temp, model){
var that = this;
that.template = _.template(ev.templateLoader.get('perfil'));
that.template2 = temp;
//console.log(this.view);
ev.router.on("route", function(route, params) {
that.$el.html(that.template2());
});
that.render();
},
render: function(map){
this.$el.html(this.template());
return this;
}
});
到目前為止,我創建了一個事件,當路由更改時,將調用我進入視圖perfil的shell模板。 但這不起作用。 我做錯了什么?
編輯:我在視圖perfil中更改了我的構造函數,以便當路由更改時僅觸發一次並調用ev.shell的render函數
ev.views.Perfil = Backbone.View.extend({
initialize: function(){
var that = this;
that.template = _.template(ev.templateLoader.get('perfil'));
ev.router.once("route", function(route, params) {
ev.shell.render();
});
that.render();
},
render: function(map){
this.$el.html(this.template());
return this;
}
});
看起來您已經准備好一個文檔,即使該文檔已加載包括地圖在內的外殼。 當您轉到個人資料頁面時,將替換#home元素的內容。 然后,當您回家時,請替換#rightcolumn元素的內容。 您永遠不會重新渲染地圖。
我認為您也需要將地圖渲染代碼放入路由器的home函數中。
作為旁注,我注意到您正在使用setTimeout函數。 如果您正在使用它,以便由於正在等待其他東西加載而渲染某些東西,那么您可能應該擺脫它並監聽事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.