Javascript requirejs一直在重新加载我的所有依赖项
我有一个Backbone.js web项目,其结构如下:Javascript requirejs一直在重新加载我的所有依赖项,javascript,backbone.js,requirejs,marionette,requirejs-text,Javascript,Backbone.js,Requirejs,Marionette,Requirejs Text,我有一个Backbone.js web项目,其结构如下: ├───container │ container.html │ ContainerView.js │ ├───home │ home.html │ HomeView.js │ └───search houses-list-template.html search.css search.html Sear
├───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(["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文本钩子会触发完全重新加载我的依赖项
以下是requirejs文本(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);
}
}
xhr.onreadystatechange=函数(evt){
var状态,err;
//不要显式地处理错误,这些错误应该是
//通过浏览器中的控制台输出可见。
if(xhr.readyState==4){
状态=xhr.status | | 0;
如果(状态>399&&状态<600){
//http 4xx或5xx错误。发出错误信号。
err=新错误(url+“HTTP状态:”+状态);
err.xhr=xhr;
如果(错误返回){
errback(err);
}
}否则{
*******************************************
*******************************************
*******************************************
*******************************************
*******************************************
***responseText是containerview.html的内容,它会触发所有模块的完全重新加载。
回调(xhr.responseText);
}
if(masterConfig.onXhrComplete){
masterConfig.onXhrComplete(xhr,url);
}
}
我在什么地方做错了什么吗?
非常感谢。RequireJS不会重新加载模块,除非您编写了取消模块定义的代码。从路径中删除
text
依赖项,只需将text.js
放在require.js旁边。当require在依赖项中看到text!
时,它将自动加载。
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);
}
}