Backbone.js: body在第二次加载时变为undefined (windows mobile)

Backbone.js : body become undefined when loaded for the 2nd time (windows mobile)

本文关键字:undefined windows mobile js body 加载 第二次 Backbone      更新时间:2023-09-26

我正在使用backbone.js和phonegap创建一个android,IOS和Windows的混合应用程序。下面是HTML、router.js和loginView.js代码:

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" href="css/jquery-ui-1.10.3.min.css"/>
    <script type="text/javascript" src="js/libs/jquery/jquery-1.10.1.min.js" ></script>
    <script type="text/javascript" data-main="js/main" src="js/libs/require/require.js"> </script>
    <script charset="utf-8" src="cordova.js"><'/script>
</head>
<body>
    <div class="loading">
            <span>Loading Data</span>
        </div>
    </div>
</body>
</html>
router code:  

define(['jquery', 'underscore', 'backbone', 'jqueryui'], 
function($, _, Backbone, jqueryui) {
var myviews = [];
var mainRouter = Backbone.Router.extend({
    routes : {
        "home" : "homepage",
        "*path" : "defaultRoute"
    }
});
var initialize = function() {
    var routrObj = new mainRouter;
    routrObj.on('route:homepage', function(actions) {
        $(myviews).each(function(ind, val) {
            val.undelegateEvents();
            val.$el.removeData().unbind();
            val.$el.remove();
            myviews.pop(val);
        });
        require(['view/landing'], function(landing) {
            var landing_new = new landing();
            landing_new.render();
            myviews.push(landing_new);
        });
    });
    routrObj.on('route:defaultRoute', function(actions) {
        $(myviews).each(function(ind, val) {
            val.undelegateEvents();
            val.$el.removeData().unbind();
            val.$el.remove();
            myviews.pop(val);
        });
        require(['view/loginView'], function(loginview) {
            var login = new loginview();
            login.render();
            myviews.push(login);
        });
    });
    Backbone.history.start();
};
return {
    initialize : initialize
};
}); 

Login View code :  
 define(['jquery', 'backbone', 'underscore', 'common', 'text!template/login.html'], function($, Backbone, _, common, loginPage) {
var loginResp, hash, self, XDeviceType = "";
var login_view = Backbone.View.extend({
    id : 'loginpage',
    /*Initializing the login view */
    initialize : function() {
        sessionStorage.clear();
        hash = CryptoJS.MD5("Skandia").toString();
    },
    /**
     * To load the login page 
     *  @param {void}   
     */
    render : function() {
        self = this;
        var compiledTemplate = _.template(loginPage);
        this.$el.append(compiledTemplate);
        var bodyDiv = $('body');
        this.$el.appendTo(bodyDiv);
    }
});
return login_view;
});

当用户尝试登出时,index.html被重新加载(使用window.location.href = 'index.html')。在Windows Mobile(Windows 8)中,当页面重新加载时,body变为未定义。因此显示空白屏幕。这个问题只发生在windows上,对于android/IOS/其他浏览器,页面显示完美。backbone .js/jquery被完美加载。无法解决问题。

任何帮助或建议都是非常感谢的。

提前感谢!

使用Backbone.history.navigate方法

onlogout点击

localStorage.clear();
sessionStorage.clear();
Backbone.history.navigate('/home', true);