Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以可优化的方式使用requirejs加载模板_Javascript_Templates_Requirejs - Fatal编程技术网

Javascript 以可优化的方式使用requirejs加载模板

Javascript 以可优化的方式使用requirejs加载模板,javascript,templates,requirejs,Javascript,Templates,Requirejs,我想为一个带有模块的项目加载所有模板,但我不确定我这样做的方式(a)是否可以让代码使用r.js进行优化,以及(b)是否合理-基本上,我担心define()块中的嵌套require不会被优化器接受。理想情况下,在运行r.js之后,我希望有一个缩小的单个文件,它是所有依赖项,包括文本文件(如果可能的话) 模板_loader.js define(["jquery","underscore","icanhaz"],function($,_,ich){ var template_names =

我想为一个带有模块的项目加载所有模板,但我不确定我这样做的方式(a)是否可以让代码使用r.js进行优化,以及(b)是否合理-基本上,我担心define()块中的嵌套require不会被优化器接受。理想情况下,在运行r.js之后,我希望有一个缩小的单个文件,它是所有依赖项,包括文本文件(如果可能的话)

模板_loader.js

define(["jquery","underscore","icanhaz"],function($,_,ich){


    var template_names = [  
        'test'
    ];

    require([
        'text!templates/test.tpl',
    ],function (){
        for (var i = arguments.length - 1; i >= 0; i--) {
            ich.addTemplate(template_names[i],arguments[i]);
        };      
    });


});
define(function(require){
    var tpl1 = require('text!generic_tpl1');
    var tpl2 = require('text!generic_tpl2');
    // etc

    return {
        tpl1: tpl1,
        tpl2: tpl2
    }
}); 
require(['generic_templates'], function(genericTemplates) {

    console.log(genericTemplates.tpl1) // some html

});
然后在我的主app.js中:

require(['jquery','underscore','backbone','icanhaz','template_loader'],function($,_,Backbone,ich,loader){

    // templates should be available here as ich.template_name()

});
目标是有一个地方处理模板资源加载,然后其他模块可以使用这些资源


这似乎是一种很好的方法吗?如果不是,还有什么更好的策略?

如果您想一次性加载所有模块,可以尝试以下方法:

通用模板.js

define(["jquery","underscore","icanhaz"],function($,_,ich){


    var template_names = [  
        'test'
    ];

    require([
        'text!templates/test.tpl',
    ],function (){
        for (var i = arguments.length - 1; i >= 0; i--) {
            ich.addTemplate(template_names[i],arguments[i]);
        };      
    });


});
define(function(require){
    var tpl1 = require('text!generic_tpl1');
    var tpl2 = require('text!generic_tpl2');
    // etc

    return {
        tpl1: tpl1,
        tpl2: tpl2
    }
}); 
require(['generic_templates'], function(genericTemplates) {

    console.log(genericTemplates.tpl1) // some html

});
app.js

define(["jquery","underscore","icanhaz"],function($,_,ich){


    var template_names = [  
        'test'
    ];

    require([
        'text!templates/test.tpl',
    ],function (){
        for (var i = arguments.length - 1; i >= 0; i--) {
            ich.addTemplate(template_names[i],arguments[i]);
        };      
    });


});
define(function(require){
    var tpl1 = require('text!generic_tpl1');
    var tpl2 = require('text!generic_tpl2');
    // etc

    return {
        tpl1: tpl1,
        tpl2: tpl2
    }
}); 
require(['generic_templates'], function(genericTemplates) {

    console.log(genericTemplates.tpl1) // some html

});

相反,您是否考虑过单独加载模块?也就是说,模块的逻辑和模板结合在一起。这将导致更易于管理和模块化的代码。当模板对应于单个模块时,这很好,但对于UI小部件或通用构建块(行、按钮等)来说,这并不是1:1感谢回复-看起来很有趣,这会允许r.js优化器将模板捆绑到一个缩小的文件中吗?是的,捆绑为单个模块,但在一个文件中。