的文件說,一旦underscore.string包括在我的應用程序,該庫可使用小號,__s全局變量(http://epeli.github.io/underscore.string/#others),或在下劃線屬性__。str或__。string。如何使用網頁全球underscore.string(與requireJS)
這個簡單的代碼都正常工作(用正確的設置配置路徑):
require(['underscore','underscore.string'], function(){
console.log(s('test').capitalize().value());
});
不過,這並不在所有的工作。這是一個簡單的jsfiddle示例。我可能做錯了什麼,但我無法弄清楚什麼。
https://jsfiddle.net/mvenrtgy/10/
相關的測試代碼:
require.config({
paths: {
'underscore':'https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min',
'underscore.string':'https://cdnjs.cloudflare.com/ajax/libs/underscore.string/3.3.4/underscore.string.min'
}
});
require(['underscore','underscore.string'], function(){
var t = 'This is a simple text I would like to SLUGIFY using unsercore.string';
// underscore is there!
console.log(typeof _)
// now check where underscore.string is...
console.log(typeof _.str);
console.log(typeof _.string);
console.log(typeof s);
console.log(typeof _s);
document.getElementsByTagName('body')[0].innerHTML = '<p>Sample text : ' + t + '</p>';
// this line will cause an error
document.getElementsByTagName('body')[0].innerHTML += '<p><em>Slugified</em> text : ' + s(t).slugify().value() + '</p>';
});
當然,我知道我們可以在下劃線內混合使用,但是我真的不希望稍後因爲下劃線干擾而陷入困境。在require調用中,我也不能像你所建議的那樣使用它,因爲我實際上需要在下劃線/骨幹模板系統中使用backbone.string函數。我忘了提到抱歉。因此,下劃線模板引擎僅適用於全局變量/函數。我別無選擇,只能找到一種方法讓underscore.string可以全局訪問.... – Simmoniz