javascript - AngularJS: View is not updating after web worker returned -


i'm using library paralleljs encryption/decryption in web worker when promise resolved doesn't update view accordly model's changes. now, know have wrap code called outside angularjs' scope in $scope.$apply, doing doesn't help.

i think reason resolving deferred object inside callback called outside angular's scope. it's little hard explain let me show code:

function _encrypt(options){ ... //crypto-js code aes encryption }  function _decrypt(options){ ... //crypto-js code 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 = [];                    (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 using jquery method execute requests outside of angularjs' scope, need wrap promise resolution in                     //the $apply method of rootscope                    $rootscope.$apply(function(){                        deferred.resolve(contacts);                    });                });             }.bind(this)).fail(function() {                $rootscope.$apply(function(){                    deferred.resolve([]);                });            });            return deferred.promise;        },    };    return new contact(); }); 

now what's happen: if leave code callback function of query method never called, since in cryptor service promise called outside angular's scope. if move $rootscope.$apply wrapper cryptor service callback inside contact service called, callback inside controller called view not updated.

any hint on how resolve this?

thanks all

a.

ok feel stupid... problem wasn't view not updating model empty. since missing bind(this) cryptor promise's callback models empty , view wasn't showing anything. changing this

cryptor.decrypt(result.cmc, key).then(function(string){     var data = json.parse(string);     var contacts = [];     (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 using jquery method execute requests outside of angularjs' scope, need wrap promise resolution in      //the $apply method of rootscope     $rootscope.$apply(function(){         deferred.resolve(contacts);     }); }); 

to this:

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

did trick.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -