2012-10-24 11 views
8

RequireJS kullanırken lodash için templateSettings ayarlamanın bir yolu var mı?Tüm gereksinimi kullanarak lodash/underscore şablon ayarlarını global olarak ayarlayın.js

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

Şu anda benim ana başlangıçta ben var

ama ben modülde _.template(...) kullandığınızda o varsayılan templateSettings kullanmak istediği için küresel templateSettings ayarlamak istediğiniz gibi görünmüyor. Sorun şu ki, bu ayarı _.template(...) kullanan her modülde değiştirmek istemiyorum.

+0

[Bu SO soru] (http://stackoverflow.com./questions/8842223/share-sources-across-different-amd-modules) bu duruma uygun bir çözüm olan bir cevabı vardır. –

cevap

16

@Tyson Phalp önerisine dayanarak, this SO question anlamına gelir.
Sorunuza uyarladım ve RequireJS 2.1.2 ve SHIM configuration'u kullanarak test ettim.

Sonra
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(); 
}); 

bunu sevdiği templateSettings ile underscoreTplSettings.js dosyası oluşturmak gerekir:

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

Yani modül underscore içerecektir
Bu main.js dosyası bu requireJS yapılandırma nerede, olduğu altyazı kütüphanesi ve şablon ayarlarınız. Başvurunuz modülleri itibaren
sadece bu şekilde, underscore modülü gerektirir:

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

ben sadece şüphe aynı sembol _ iki kez ihraç olmam, bile zor bu iş emin değilim bu iyi bir uygulama olarak kabul edilirse.

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

ALTERNATİF ÇÖZÜM: Bu aynı zamanda iyi çalışıyor ve bunu biraz sanırım Yukarıdaki çözüm olarak ekstra bir modül oluşturmayı ve gerektirmeyi önleyerek daha temiz. Bir başlatma işlevini kullanarak Shim yapılandırmasında 'dışa aktarma' değiştirdim. Daha fazla bilgi için Shim config reference'a bakın.

//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

ama "underscoreBase" ihracatını değiştiremediniz mi? Bu çözümü gerçekten çok beğendim. Çok küçük bir sarıcı ile güzel ve temiz. – milkypostman

+1

Haklısınız, yukarıdaki kodda alternatif bir çözüm ekledim. – Leonardo

+0

Eğer requirejs-tpl eklentisi olan Marionette kullanıyorsanız (https://github.com/ZeeAgency/requirejs-tpl) templateSettings = _.templateSettings || {...}; tpl.js'nin 26. satırına ... Bu, RequireJS ortamlarında yukarıdaki bu yararlı çözümün boogery override edilmesini önler. – toszter

0

Sen fonksiyon argüman olarak veya global nesne (tarayıcılar için pencere veya nodejs için proccess) 'de özellik olarak şablon ayarlarıyla _ değişkeni geçmelidir.

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

Ya

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

İlk seçenek daha iyidir.

+0

ama o zaman gerçek görünümün sonuna kadar devam etmem gerekiyor. Bunu kullanmak için kullanacağım çünkü bir Koleksiyon içeren bir Görünümüm var, Koleksiyondaki her öğenin kendi Görünümü ve koleksiyon öğelerinin tek görünümleri aslında şablon oluşturma işlemidir. – milkypostman

+0

Yapmanız gereken gerçek, ancak diğer nesneye görünümünüzden erişebiliyorsanız, bu nesneye "ekleyebilir". Sigle view nesnesinin prototipini ayarlayabilirsiniz, böylece her nesne için bir kere tanımlayamazsınız: "collectionElementViewObject.constructor.prototype._ = _;" veya "collectionElementView.prototype._ = _;" Alternatif Global değişken veya singleton kayıt defteri benzeri bir depolama kullanmaktır. Ama kötü bir uygulama var. –

0

Ayı akılda Eğer altını kullanıyorsanız> = 1.6.0 veya lodash-amd çözüm oldukça basittir:

"main.js" yapılandırma dosyası

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', 
    } 
}); 

Sizin "_.js: çizgi "ve onu yamaları _ "gerektiren modülü"" dosyası.

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 _; 
}); 

bir modül dosyası Bizim gerektiriyor"

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