【问题标题】:Backbone.js - using browser back button router not initiatingBackbone.js - 使用浏览器后退按钮路由器未启动
【发布时间】:2013-05-18 07:18:53
【问题描述】:

在我的应用中..

当我加载页面或刷新页面时,我的路由器没有正确响应并调用默认视图方法。如果我使用其他链接来访问其他哈希 url... 也可以正常使用后退按钮。

即使在我加载页面时,我的启动功能也可以正常工作。但是当我加载页面并使用浏览器的后退按钮时,默认方法不会触发。

这是什么原因..这是我的代码:

    define([
    "backbone",
    "../model/model",
    "../collection/collection",
    "../views/appView",
    "../views/listView",
    "../views/appViews"], 
    function(Backbone,appModel,collection,appView,listView,appViews){

    var appRouter = Backbone.Router.extend({
        routes:{
            "":"defaultView",
            "list":"listAItem",
            "add":"listViewIt"
        },
        initialize:function(){
            console.log("initiated....."); //works properly
        },
        defaultView:function(){
            new appViews(); // not working..
        },
        listAItem:function(){
            console.log("from listAItem"); 
                    // on click and using back button woks fine
        },
        listViewIt:function(){
            new listView();
                   // on click and using back button woks fine
        }
    });

    Backbone.history.start(); // removed and updated to where i call my app...

    return appRouter;
})

现在我更新了我的 Backbone.history 以在启动我的应用程序后调用..现在我的初始方法(调用默认视图)但是当我在浏览器中使用我的后退/字体按钮时仍然无法正常工作..但重置工作正常。 .

如何解决..

这是我更新的代码:

 $.when.apply(null,requests).done(function(){
        var app = new router(); 
       //on refresh works, but not working while use back / front button of browser use.
        Backbone.history.start();
    });

【问题讨论】:

标签: backbone.js backbone-routing


【解决方案1】:

有效...

将初始元素设置为删除是我对其他视图的错误。现在我意识到了。现在工作正常。谢谢大家。。

更新代码:

this.$el.find("ul").html("<h1>New html has to come here</h1>");

它被错误地写成:

this.$el.html("<h1>New html has to come here</h1>");

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 2012-05-10
    • 2012-05-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2023-04-05
    相关资源
    最近更新 更多