2013-12-22 1 views
0

"전역 모듈 범위"에 대해 async.queue를 초기화 할 수 있습니까? 아래 예제에서는 qq이 정의되지 않았거나 아직 알려지지 않았거나 함수 범위에서만 로컬로 정의 된 주요 문제를 보여줍니다.모듈 패턴으로 초기화하는 방법

목표는 다른 모듈 멤버 함수의 "module-global"q에 액세스하는 것입니다. 그래서 예제의 모듈 패턴 버전을 작성하십시오

// not working -code가 유효하지 않은 이유를 알고 있습니다. 그 이유는 제가 시도한 선언 아이디어 만 보여주기 때문입니다.

추가 다른 패턴을 사용하여 문제를 해결하는 방법을 알고 있지만 질문에 대답하지 않습니다.

var mymodule = (function() { 
    'use strict'; 

    var async = require('async'); 
    // var q = async.queue(mymodule.qq); // not working 
    // var q ; // not working 
    var mymodule = { 

     // q = async.queue(this.qq); // not working 
     init: function() { 
      // var q = async.queue(this.qq); // local not global 
      // q = async.queue(this.qq); // not working 
      q.drain = function() { 
       console.log('all items have been processed'); 
      } 
     }, 

     add: function(task) { 
      this.q.push(task); 
     }, 

     qq: function(task, callback) { 
      console.log(task); 
      callback(); 
     }, 

    }; 
    return mymodule; 
}()); 

답변

1
'use strict'; 
var async = require('async'); 
var mymodule = function(){ 

//This will be you constructor 
//You can do something like this 
    this.queue = async.queue(function(task, callback){ 
    console.dir(task); 
    }, 4); 
}; 
//Now start adding your methods 
mymodule.prototype.add = function(task){ 
    this.queue.push(task, function(){}); 
}; 

mymodule.prototype.qq = function(task, callback){ 
// .. 
callback() 
}; 
//export it 

module.exports = mymodule; 
+0

나는 모듈에서 비동기를 분리하는 큐를 초기화하는 데 도움이 표시되지 않습니다, 중 내가 var에 Q = async.queue (mymodule.qq) 같은 것을 찾을 수 없습니다. 그리고 주요 아이디어 중 하나는 "모든 것을 모듈 범위로 유지"하지 못했습니다. – inselberg

관련 문제