2012-10-24 13 views
8

का उपयोग कर वैश्विक स्तर पर lodash/underscore टेम्पलेट सेटिंग्स सेट करें RequireJS का उपयोग करते समय lodash के लिए templateSettings सेट करने का कोई तरीका है?require.js

अभी अपने मुख्य स्टार्टअप में मेरे पास है

,

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/शेयर-संसाधन-अलग-अलग-एएमडी-मॉड्यूल) का एक उत्तर है जो इस स्थिति के लिए व्यवहार्य समाधान है। –

उत्तर

16

@ टायसन फाल्प सुझाव के आधार पर, इसका अर्थ है 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! 
     } 
    } 
} 
+1

लेकिन क्या आप सिर्फ 'अंडरस्कोरबेस निर्यात' को बदल नहीं सकते? हालांकि मुझे वास्तव में यह समाधान पसंद है। बहुत ही मामूली रैपर के साथ अच्छा और साफ। – milkypostman

+1

आप सही हैं, मैंने उपरोक्त कोड में एक वैकल्पिक समाधान जोड़ा है। – Leonardo

+0

यदि आप requjs-tpl प्लगइन (https://github.com/ZeeAgency/requirejs-tpl) के साथ मैरियनेट का उपयोग कर रहे हैं तो टेम्पलेट सेटिंग जोड़ने पर विचार करें _ _ememplateSettings || {...}; tpl.js की पंक्ति 26 के लिए ... यह RequJS वातावरण में उपरोक्त इस उपयोगी समाधान के बूगरी ओवरराइड को रोकता है। – toszter

0

आप समारोह तर्क के रूप में या वैश्विक वस्तु (ब्राउज़र के लिए खिड़की या NodeJS के लिए प्रक्रिया) में संपत्ति के रूप में टेम्पलेट सेटिंग्स के साथ अपने _ चर पास करना चाहिए।

_.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); 
}); 
संबंधित मुद्दे