次のような構造の 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) 内のコードです (「コールバック」呼び出しを確認してください)。これにより、すべてのモジュールの完全なリロードがトリガーされ、最終的には Application インスタンスが範囲外になります。これにより、すべてが壊れます。私のアプリケーションの状態。
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);
}
}
私はどこかで何か間違っていますか?どうもありがとう。