Backbone.js 需要模块的正确方式是什么
考虑以下requirejs配置:Backbone.js 需要模块的正确方式是什么,backbone.js,requirejs,Backbone.js,Requirejs,考虑以下requirejs配置: requirejs.config({ waitSeconds: 10, baseUrl: 'js/libs', paths: { 'jQuery' : 'jquery', 'jQueryUi' : 'jquery.ui', 'cookie' : 'jquery.cookie', 'underscore'
requirejs.config({
waitSeconds: 10,
baseUrl: 'js/libs',
paths: {
'jQuery' : 'jquery',
'jQueryUi' : 'jquery.ui',
'cookie' : 'jquery.cookie',
'underscore' : 'underscore',
'backbone' : 'backbone',
'text' : 'text',
'reusable' : '../../tmpl/reusable.tmpl.html'
},
shim: {
'jQuery':{
exports: '$'
},
'cookie':['jQuery'],
'jQueryUi':['jQuery'],
'underscore': {
exports: '_'
},
'backbone':{
deps: ['underscore'],
exports: 'Backbone'
}
}
});
require(['cookie','jQueryUi'],function(){
require(['backbone'],function(){
this._templates = {};
require(['text!reusable'],function(reusable){
this._templates['reusable'] = reusable;
});
});
});
到目前为止一切正常
部分_module.js的内容:
define(function(){
var init = function(){
console.log('some_module initialized');
};
return{
init:init
};
});
基于主干路由器导航功能,在某个特定点需要一些_模块
,例如:
require(['some_module'],function(module){
module.init();
});
在路线更改时,类似的模块以相同的方式调用
导航时,用户可能返回到上一条路线,这意味着再次请求某个模块
,这一次是从缓存
,因为在第一次调用时某个模块.js
存储在那里
问题:
由于可以请求一次或多次某些_模块
,因此每次加载模块(即使文件已缓存)是否是一种良好的做法?。。。或者最初在全局范围内设置它:
var some_module = require('some_module');
每次需要模块时,只需调用:
some_module.init()
或者它可能归还的任何财产
内存泄漏和一般应用程序功能有什么区别?一般规则是,通过RequireJS加载的所有内容都加载一次,并且只加载一次 第一次需要模块时(由于
require
或将模块作为依赖项的define
),将实际从网络获取模块,然后执行其出厂功能。(工厂是您赋予define
的函数)工厂函数的结果由RequireJS缓存
当再次需要该模块时,该模块将从RequireJS缓存返回,而不使用浏览器的文件缓存或进入网络。因此,将其定义为全局范围是不可能的。。。非常有用的见解。认为一旦加载,每次都会从浏览器缓存调用模块。谢谢。作为正常操作的一部分,我绝对不会向全球范围泄漏模块。