gpt4 book ai didi

javascript - Backbone : Render view that has been created before

转载 作者:行者123 更新时间:2023-11-30 17:07:30 25 4
gpt4 key购买 nike

我有包含导航栏和 map 的 shell View 。向此 View 呈现使用先前呈现的 map 的其他 View 。当我转到 View perfil 时, map 被删除,但导航栏保持不变,到目前为止一切顺利。我的问题是当回到家时, map 没有出现,只出现包含 map 的 div。下面显示示例:

查看Shell并查看Home: enter image description here

去查看Perfil: enter image description here

回到家:

enter image description here

这是我的代码:

应用程序.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;
}
});

到目前为止,我创建了一个事件,当路由更改时,将调用我进入 View perfil 的 shell 模板。但它不起作用。我做错了什么?

编辑:我在 View perfil 中更改了我的构造函数,以便在路由更改时仅触发一次并调用 ev.shell 的渲染函数

 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;
}
});

最佳答案

看起来您已经准备好文档,甚至加载了包含 map 的 shell。当您转到配置文件页面时,您将替换#home 元素的内容。然后,当您回到家时,您将替换#rightcolumn 元素的内容。您永远不会重新渲染 map 。

我认为你也需要将 map 渲染代码放入路由器的home函数中。

作为旁注,我注意到您正在使用 setTimeout 函数。如果您正在使用它以便渲染某些东西,因为它正在等待其他东西加载,那么您可能应该摆脱它并监听一个事件。

关于javascript - Backbone : Render view that has been created before,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27691631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com