0

我有一个结构如下的 Backbone.js Web 项目:

 ├───container
 │       container.html
 │       ContainerView.js
 │
 ├───home
 │       home.html
 │       HomeView.js
 │
 └───search
         houses-list-template.html
         search.css
         search.html
        SearchView.js
 index.html

index.html 包含所有 requirejs 定义,例如:

 <script type="application/javascript">
requirejs.config({
    baseUrl: 'js',
    paths: {
        text: "requirejs-text-2.0.14",
        css: "css",
        jquery: "jquery-1.11.3.min",
        jquerycolor: "jquery.color",
        cookies: "js.cookie",
        q: "q",
        container:"../views/container/ContainerView",
        home: "../views/home/HomeView",
        search: "../views/search/SearchView",

我所有的子视图都是这样的:

 define([
"jquery",
"backbone",
'text!../views/container/container.html',    
"i18next",
"services",
"q"],
function ($,Backbone, view, i18n,services,Q) {

    var  ContainerView = Backbone.View.extend({
        // many methods
    });

   return ContainerView;});

现在,启动应用程序的脚本类似于

 define(["applicationdef"],function (Application) {

    if(window.application ===null || window.application === undefined){
        window.application = new Application({container: '#container'});
        window.application.start();
    }
});

在我的应用程序逻辑中,我有一个 ContainerView ,定义了附加和分离子视图。

现在,视图之间的导航由主干路由器调解,它拦截 URL 更改并触发操作。一个动作被触发,URL 改变,ContainerView 交换一个视图并放入另一个视图。随着 ContainerView 中的内容发生更改,ContainerView.html 上的 requirejs-text 挂钩会触发我的依赖项的完全重新加载。

这是requirejs-text(2.0.14版)中的代码(检查“回调”调用“)。它触发了所有模块的完全重新加载,最终它还导致应用程序实例超出范围。这打破了所有我的应用程序的状态。

  xhr.onreadystatechange = function (evt) {
            var status, err;
            //Do not explicitly handle errors, those should be
            //visible via console output in the browser.
            if (xhr.readyState === 4) {
                status = xhr.status || 0;
                if (status > 399 && status < 600) {
                    //An http 4xx or 5xx error. Signal an error.
                    err = new Error(url + ' HTTP status: ' + status);
                    err.xhr = xhr;
                    if (errback) {
                        errback(err);
                    }
                } else {
                    *******************************************   
                    *******************************************
                    *******************************************
                    *******************************************
                    *******************************************
                    *** the responseText is the content of containerview.html and it triggers a full reload of all the modules. 
                    callback(xhr.responseText);
                }

                if (masterConfig.onXhrComplete) {
                    masterConfig.onXhrComplete(xhr, url);
                }
            }

我在某处做错了吗?非常感谢。

4

0 回答 0