2017-02-14 9 views
0

을 underscore.string 사용 방법, 라이브러리는 , __s 전역 변수 (http://epeli.github.io/underscore.string/#others)를 사용하여 사용할 수 있으며, 또는 내 밑줄 속성 __ str 또는 __ 문자열.는 문서가 한 번 내 응용 프로그램에 포함 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>'; 


}); 

답변

0

underscore.string는 잘 동작하는 AMD 라이브러리입니다.

require(['underscore','underscore.string'], function(_, s){ 

를 당신의 코드에이 변경을함으로써 내가 작동 뭔가를 얻을 : 당신이로드 모듈에 해당하는 매개 변수를 얻을 수 있도록 당신은 당신의 require 전화를 변경해야합니다.

Slugified 텍스트 : 테스트의 두번째 라인으로 등장이-IS-A는 단순 텍스트-I-것 같은 -에 - slugify - 사용 - unsercore 문자열

underscore.string underscore 확장으로을 시작하면서 또한

는, 그 이상으로 성장했다, 그래서 당신은 당신이 underscore 함께있는 혼합 돌봐 하지 않는 한, 글로벌 공간으로 _ 글로벌 underscore 그 누출을 통해 액세스 할 수 없습니다.

_.mixin(s.exports()); 

을하지만 underscore.string이 기본 기능 underscore 수출의 일부를 방해하기 때문에 문서는 권장하지 다음 documentation은 당신이 할 수 있다고 말한다. 자신의 위험에 사용하십시오.

+0

필자는 밑줄을 사용하여 혼합 할 수 있지만 실제로는 나중에 언더 스코어에 간섭을 받고 싶지는 않습니다. 밑줄/백본 템플리트 시스템 내에서 backbone.string 함수를 실제로 사용해야하기 때문에 필 요한 호출에서 제안한 것처럼 사용할 수 없습니다. 나는 미안하다고 언급하는 것을 잊었다. 따라서 밑줄 템플릿 엔진은 전역 변수/함수에서만 작동합니다. 나는 선택의 여지가 있지만 밑줄을 긋는 방법을 찾는다. 세계적으로 접근 가능하다. – Simmoniz