Javascript 主干:以前创建的渲染视图

Javascript 主干:以前创建的渲染视图,javascript,backbone.js,backbone-views,backbone-events,backbone-routing,Javascript,Backbone.js,Backbone Views,Backbone Events,Backbone Routing,我有一个包含导航栏和地图的shell视图。将使用先前渲染的贴图渲染到此视图的其他视图。当我进入查看perfil时,地图被移除,但导航栏被保留,目前为止还不错。我的问题是,当返回主页时,地图不会显示,只会显示包含地图的div。下面显示示例: 查看Shell和查看Home: 转到查看性能: 回到家: 这是我的密码: app.js var ev = new Application(); ev.Router = Backbone.Router.extend({ routes: {

我有一个包含导航栏和地图的shell视图。将使用先前渲染的贴图渲染到此视图的其他视图。当我进入查看
perfil
时,地图被移除,但导航栏被保留,目前为止还不错。我的问题是,当返回主页时,地图不会显示,只会显示包含地图的
div
。下面显示示例:

查看Shell和查看Home:

转到查看性能:

回到家:

这是我的密码:

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模板。但它不起作用。我做错了什么

编辑: 我在view 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;
        }
    });

看起来您已经准备好了一个文档,甚至可以加载包含映射的shell。转到profile页面时,将替换#home元素的内容。然后,返回主页时,替换#rightcolumn元素的内容。您永远不会重新渲染贴图

我认为您还需要将地图渲染代码放入路由器的home函数中


作为旁注,我注意到您正在使用setTimeout函数。如果您正在使用此选项,因为它正在等待加载其他内容,所以某些内容会呈现,那么您可能应该将其删除并收听事件。

感谢您的解释,我对如何解决此问题变得更清楚了。我在视图
perfil
中更改了构造函数,这样当路由更改时,只触发一次并调用
ev.shell的
render
函数