2013-09-04 3 views
0

웹 작업자에서 ParallelJS 라이브러리를 사용하여 암호화/암호 해독을 수행하고 있지만 약속이 해결되면 모델 변경 사항에 따라보기가 업데이트되지 않습니다. 자, 나는 $ scope에서 anglejs의 범위 밖에있는 코드를 감싸 야한다는 것을 알고 있습니다. $ apply,하지만 이것을해도 도움이되지 않습니다.AngularJS : 웹 작업자가 돌아온 후에보기가 업데이트되지 않습니다.

나는 각도의 범위 밖이라고 불리는 콜백 내부에서 지연된 객체를 해결하고 있다고 생각합니다. 의 일이 무엇인지 이제

function _encrypt(options){ 
... //crypto-js code to do AES encryption 
} 

function _decrypt(options){ 
... //crypto-js code to do AES decryption 
} 

angular.module('CryptoService', []).factory('Cryptor', function($q, $rootScope){ 
    function Cryptor(){}; 
    Cryptor.prototype = { 
     encrypt: function(string, key) { 
      var deferred = $q.defer(); 
      var ivS = generateIV(); 
      var p = new Parallel({ 
       iv: ivS, 
       text: string, 
       key: key 
      }, { evalPath: '/assets/js/eval.min.js' }); 

      p.require('/assets/js/crypto.min.js'); 

      p.spawn(_encrypt).then(function(result){ 
       deferred.resolve(result); 
      }); 

      return deferred.promise; 
     }, 
     decrypt: function(string, key) { 
      var deferred = $q.defer(); 
      var p = new Parallel({ 
       text: string, 
       key: key 
      }, { evalPath: '/assets/js/eval.min.js' }); 

      p.require('/assets/js/crypto.min.js'); 

      p.spawn(_decrypt).then(function(result){ 
       deferred.resolve(result); 
      }); 
      return deferred.promise; 
     } 
    }; 
    return new Cryptor(); 
}); 

angular.module('ContactService', ['CryptoService']).factory('Contact', function($q, $rootScope, Cryptor){ 
    function Contact(){ 
     //initialization 
    }; 
    Contact.prototype = { 
     query: function(){ 
      var deferred = $q.defer(); 
      var options = {}; 
      _oauth.secureGET(this._endpoint,options).done(function(result){ 
       Cryptor.decrypt(result.cmc, key).then(function(string){ 
        var data = JSON.parse(string); 
        var contacts = []; 
        for (var cidx in data){ 
         var objContact = data[cidx]; 
         var c = new Contact(); 
         for(var pidx in this._properties){ 
          var property = this._properties[pidx]; 
          c[property] = objContact[property]; 
         } 
         contacts.push(c); 
        } 
        //Since _oauth is using a jQuery method to execute the requests we are outside of angularjs' scope, so we need to wrap the promise resolution in 
        //the $apply method of the rootscope 
        $rootScope.$apply(function(){ 
         deferred.resolve(contacts); 
        }); 
       }); 

      }.bind(this)).fail(function() { 
       $rootScope.$apply(function(){ 
        deferred.resolve([]); 
       }); 
      }); 
      return deferred.promise; 
     }, 
    }; 
    return new Contact(); 
}); 

: 내가 코드를 떠날 경우 cryptor 서비스의 약속은 외부에서 호출되기 때문에 쿼리 방법의 콜백 함수가 호출되지 않습니다 그것은 나를 내 코드를 표시 할 수 있도록 설명하기 조금 어렵다 각의 범위. Cryptor 서비스에 $ rootscope. $ apply 래퍼를 이동하면 Contact 서비스 내의 콜백이 호출되고 컨트롤러 내부의 콜백이 호출되지만 뷰는 업데이트되지 않습니다.

해결 방법에 대한 힌트가 있습니까?

감사합니다 당신에게 당신의 코드에서 모든

A.

답변

1

좋아, 내가 너무 바보가 된 기분 통지

위해
$timeout(function(){ 
    deferred.resolve(result); 
}, 0) 

에 ... 문제는 '년후 뷰는 업데이트되지 않지만 모델은 비어 있습니다. Cryptor 약속의 콜백에 bind(this)이 누락 되었기 때문에 모델이 비어 있었고보기에 아무 것도 표시되지 않았습니다. 이이

Cryptor.decrypt(result.cmc, key).then(function(string){ 
    var data = JSON.parse(string); 
    var contacts = []; 
    for (var cidx in data){ 
     var objContact = data[cidx]; 
     var c = new Contact(); 
     for(var pidx in this._properties){ 
      var property = this._properties[pidx]; 
      c[property] = objContact[property]; 
     } 
     contacts.push(c); 
    } 
    //Since _oauth is using a jQuery method to execute the requests we are outside of angularjs' scope, so we need to wrap the promise resolution in 
    //the $apply method of the rootscope 
    $rootScope.$apply(function(){ 
     deferred.resolve(contacts); 
    }); 
}); 

변경 :

Cryptor.decrypt(result.cmc, key).then(function(string){ 
    ... 
}.bind(this)); 

트릭을했다.

0

: Parallel.js에서

p.spawn(_encrypt).then(function(result){ 
    deferred.resolve(result); 
}); 

약속 같은 약속하지 않은 Angular.js한다. 그래서 당신은 Angular.js $ 제한 시간에

deferred.resolve(result); 

을 포장해야이 Angular.js

+0

나중에 (연락 서비스에서) $ rootScope. $ apply 메서드 내에서 다른 약속을하면 코드가 double $ digest 오류가 발생합니다. $ rootScope. $ apply를 제거하면 컨트롤러까지 약속을 해결할 수 있지만보기가 업데이트되지 않습니다. –

+0

$ digest 오류 :보세요 http://stackoverflow.com/questions/12729122/prevent-error-digest-already -in-progress-when-calling-scope-apply 이것은 내가 $ timeout 대신 $ apply를 사용하도록 제안한 이유입니다. – kostik

+0

당신이 말한 것을 이해 했습니다만, $ timeout은 $ digest를 강제하기 때문에 나는 그것을 사용할 수 없습니다. 나는 이후에 $ 신청을한다 –

관련 문제