@ टायसन फाल्प सुझाव के आधार पर, इसका अर्थ है this SO question।
मैंने इसे आपके प्रश्न पर अनुकूलित किया और मैंने इसे RequJS 2.1.2 और SHIM configuration का उपयोग करके परीक्षण किया।
यह main.js
फ़ाइल है कि है, जहां requireJS config है:
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();
});
तो फिर तुम इतने तरह अपने templateSettings साथ underscoreTplSettings.js
फ़ाइल बनाने चाहिए:
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 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!
}
}
}
[यह तो सवाल] (http://stackoverflow.com।/प्रश्न/8842223/शेयर-संसाधन-अलग-अलग-एएमडी-मॉड्यूल) का एक उत्तर है जो इस स्थिति के लिए व्यवहार्य समाधान है। –