2015-01-07 1 views
-1

이 응용 프로그램에 필요한 두 개의 밑줄 혼합에 대한이 requireJs 모델을 작성했습니다. require.config에 파일을 추가했는데 파일이로드되었지만 두 함수 중 하나를 사용하려고하면 정의되지 않았습니다. 내가 잘못하고있는 것을 알고 있지만 나는 무엇을 모른다.RequireJs 모듈의 UnderscoreJs Mixins

define(['underscore.mixins'], function (module) 
{ 
    require(['underscore'], function (_) { 
     _.mixin({ 
      'toQueryString': function (parameters) { 
       var queryString = _.reduce(
        parameters, 
        function (components, value, key) { 
         components.push(key + '=' + encodeURIComponent(value)); 
         return components; 
        }, 
        [] 
       ).join('&'); 
       if (queryString.length > 0) { 
        queryString = '?' + queryString; 
       } 
       return queryString; 

업데이트 1 :

나는 그것을 수정하지만 여전히 나를 위해 작동하지 않습니다.
I했던이 :
main.js :

require([ 
    '../common/requireConfig' 
], function() { 
    'use strict'; 

    requirejs.s.contexts._.config.paths.chai = 'thirdParty/chai'; 
    requirejs.s.contexts._.config.paths.mocha = 'thirdParty/mocha'; 
    requirejs.s.contexts._.config.paths.sinon = 'thirdParty/sinon'; 

    requirejs.s.contexts._.config.shim.mocha = { 
     exports: 'window.mocha' 
    }; 

    requirejs.s.contexts._.config.shim.sinon = { 
     exports: 'window.sinon' 
    }; 

    // Then, load all of the plugins needed by test: 
    require(['test/plugins']); 
}); 

requireconfig.js

define(function() { 
    'use strict'; 

    require.config({ 
     baseUrl: 'js/', 
     enforceDefine: true, 

     paths: { ... } 
}); 


require(["underscore", "thirdParty/underscore.mixins"], function (_) { 
    _.toQueryString({}); 
}); 

하지만이 함수를 호출 : 모델 파일에

requestUrl += _.toQueryString(_.extend({ 
        key: YouTubeAPIKey 
       }, ajaxDataOptions));\ 

을 아직 정의되지 않았습니다.


underscore.mixins.js :

define(['underscore'], function (_) { 
    _.mixin({ 
     'toQueryString': function (parameters) { 
      var queryString = _.reduce(
       parameters, 
       function (components, value, key) { 
        components.push(key + '=' + encodeURIComponent(value)); 
        return components; 
       }, 
       [] 
      ).join('&'); 
      if (queryString.length > 0) { 
       queryString = '?' + queryString; 
      } 
      return queryString; 
     }, 

     'fromQueryString': function (queryString) { 
      return _.reduce(
       queryString.replace('?', '').split('&'), 
       function (parameters, parameter) { 
        if (parameter.length > 0) { 
         _.extend(parameters, _.object([_.map(parameter.split('='), decodeURIComponent)])); 
        } 
        return parameters; 
       }, 
       {} 
      ); 
     } 
    }); 

    return _; 
}); 

그리고 실제로 그 기능을 사용하는 파일의 맨 위에 :

을 업데이트 2

이 작업을 수행하여 작업을 알았어요

define([ 'background/collection/songs', 'background/key/youTubeAPI', 'background/model/song', 'common/enum/songType', 'common/enum/youTubeServiceType', 'common/utility', 'thirdParty/underscore.mixins' ], function (Songs, YouTubeAPIKey, Song, SongType, YouTubeServiceType, Utility,_) { 
+0

나는 다시 처음에 무엇이 질문을 압연 추천 할 것입니다. 그 때 명확한 질문이있었습니다. 이후에 추가 한 모든 것은 방금 물건을 어둡게 만들었습니다. 당신은 이미 downvote와 투표 마감을 축적했습니다. 사람들이 하향 투표를 계속하지 않더라도 ** 다른 사람들이 귀하의 질문을 "현재,이 문제가 너무 있음"이라고 말하면서 귀하의 질문을 상대로 회피 할 가능성은 거의 없습니다 **. – Louis

답변

1

define 안에있는 require은 완전히 불필요한 것으로 보이며 실제로 다른 모듈이 모듈이로드되면 믹스가 실제로으로 등록되었다고 보장 할 수 없기 때문에이 모듈을 사용하십시오. 이는 require이 비동기식이기 때문에 코드 require이 전달되기 전에 define으로 반환되기 때문입니다.

나는 이런 식으로 언더 코어에 믹스 인을 추가 할 수 있습니다. 모듈이 foo을 얻 자마자 mixin이 Underscore에 등록되어 있음을 보증합니다.

foo.js

:

define(["underscore"], function (_) { 

_.mixin({ 
    foo: function() { 
     console.log("foo invoked"); 
    } 
}); 

}); 

index.html :

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/> 
    <script type="text/javascript" src="js/require.js"></script> 
    </head> 
    <body> 
    <script> 
     require.config({ 
     baseUrl: "js" 
     }); 
     require(["underscore", "foo"], function (_) { 
     _.foo(); 
     }); 
    </script> 
    </body> 
</html> 
+0

도움을 주셔서 감사하지만 불행히도 그것은 여전히 ​​나를 위해 작동하지 않습니다. 업데이트 1을 참조하십시오. – Para

+0

return _;을 추가하여 작동하게했습니다. in foo.js. 업데이트 2를 참조하십시오. 이것이 다른 방법으로 더 잘 수행 될 수 있다면 알려주십시오. – Para