2012-10-24 58 views
8

當使用RequireJS時,有沒有辦法將templateSettings設置爲lodash使用require.js全局設置lodash /下劃線模板設置

現在在我的主啓動我有,

require(['lodash', 'question/view'], function(_, QuestionView) { 
    var questionView; 
    _.templateSettings = { 
     interpolate: /\{\{(.+?)\}\}/g, 
     evaluate: /\{\%(.+?)\%\}/g 
    }; 
    questionView = new QuestionView(); 
    return questionView.render(); 
    }); 

,但它似乎並不希望設置templateSettings全球,因爲當我在一個模塊中使用_.template(...)它要使用默認templateSettings。問題是我不想在每個使用_.template(...)的模塊中更改此設置。

+0

[這太問題(http://stackoverflow.com。 /問題/ 8842223 /股的資源,跨不同,AMD-模塊)有一個答案是一個可行的解決這種情況。 –

回答

16

根據@Tyson Phalp的建議,這意味着this SO question
我將它改編成你的問題,我使用RequireJS 2.1.2和SHIM configuration對它進行了測試。
這是main.js文件,即在requireJS配置爲:

require.config({ 
/* The shim config allows us to configure dependencies for 
    scripts that do not call define() to register a module */ 

    shim: { 
     underscoreBase: { 
     exports: '_' 
     }, 
     underscore: { 
     deps: ['underscoreBase'], 
     exports: '_' 
     } 

    }, 
    paths: { 
     underscoreBase: '../lib/underscore-min', 
     underscore: '../lib/underscoreTplSettings', 
    } 
}); 

require(['app'],function(app){ 
    app.start(); 
}); 

那麼你應該創建underscoreTplSettings.js文件與templateSettings像這樣:

define(['underscoreBase'], function(_) { 
    _.templateSettings = { 
     evaluate: /\{\{(.+?)\}\}/g, 
     interpolate: /\{\{=(.+?)\}\}/g, 
     escape: /\{\{-(.+?)\}\}/g 
    }; 
    return _; 
}); 

所以你的模塊underscore將包含下劃線庫和你的模板設置。
從您的應用程序模塊只需要underscore模塊,以這樣的方式

define(['underscore','otherModule1', 'otherModule2'], 
    function(_, module1, module2,) { 
     //Your code in here 
    } 
); 

我唯一的疑問是,我出口的相同符號_兩次,甚至強硬這項工作我不知道如果這被認爲是一種好的做法。

=========================

替代解決方案: 這也工作正常,我想這是一個有點作爲上述解決方案,避免創建並需要額外的模塊。我使用初始化函數更改了Shim配置中的「導出」。欲瞭解更多信息,請參閱Shim config reference

//shim config in main.js file 
shim: {  
    underscore: { 
     exports: '_', 
     init: function() { 
     this._.templateSettings = { 
      evaluate:/\{\{(.+?)\}\}/g, 
      interpolate:/\{\{=(.+?)\}\}/g, 
      escape:/\{\{-(.+?)\}\}/g 
     }; 
     return _; //this is what will be actually exported! 
     } 
    } 
} 
+1

,但是難道你不能改變'underscoreBase'的輸出嗎?雖然我很喜歡這個解決方案。很好,乾淨,很小的包裝。 – milkypostman

+1

你是對的,我在上面的代碼中添加了一個替代解決方案。 – Leonardo

+0

在你使用的木偶與requirejs-TPL插件的情況下(https://github.com/ZeeAgency/requirejs-tpl)考慮加入templateSettings = _.templateSettings || {...};到tpl.js的第26行...這可以防止RequireJS環境中上述有用解決方案的boogery覆蓋。 – toszter

0

你應該用模板設置爲函數參數或在全局對象(窗口的瀏覽器或的NodeJS proccess)財產傳給你_變量。

_.templateSettings = { 
     interpolate: /\{\{(.+?)\}\}/g, 
     evaluate: /\{\%(.+?)\%\}/g 
}; 
questionView = new QuestionView(_); 

或者

_.templateSettings = { 
     interpolate: /\{\{(.+?)\}\}/g, 
     evaluate: /\{\%(.+?)\%\}/g 
}; 
window._ = _ 

第一個選項是更好的。

+0

但後來沒有我必須保持一路向下傳遞到實際的看法,多數民衆贊成要使用它嗎?因爲我有一個包含集合的視圖,集合中的每個元素都有自己的視圖,而集合元素的單個視圖就是實際呈現模板的內容。 – milkypostman

+0

當然你應該,但是如果你可以從視圖中訪問其他對象,你可以「附加」_到那個對象。您可以設置_原型您的事務所視圖對象,所以你會再定義它,而不是爲每個對象:'collectionElementViewObject.constructor.prototype._ = _;'或'collectionElementView.prototype._ = _;' 替代要麼使用全局變量,要麼使用單一類似於註冊表的存儲。但這是一個糟糕的做法。 –

0

記住,如果你使用下劃線> = 1.6.0或lodash,AMD的解決方案很簡單:

「main.js」 配置文件

require.config({ 
    baseUrl: './', // Your base URL 
    paths: { 
    // Path to a module you create that will require the underscore module. 
    // You cannot use the "underscore" name since underscore.js registers "underscore" as its module name. 
    // That's why I use "_". 
    _: 'underscore', 

    // Path to underscore module 
    underscore: '../../bower_components/underscore/underscore', 
    } 
}); 

你的 」_。JS」文件:

define(['underscore'], function(_) { 

    // Here you can manipulate/customize underscore.js to your taste. 
    // For example: I usually add the "variable" setting for templates 
    // here so that it's applied to all templates automatically. 

    // Add "variable" property so templates are able to render faster! 
    // @see http://underscorejs.org/#template 
    _.templateSettings.variable = 'data'; 

    return _; 
}); 

模塊文件它要求我們 「_」 模塊要求 「下劃線」 和補丁它

define(['_'], function(_){ 
    // You can see the "variable" property is there 
    console.log(_.templateSettings); 
});