0

I write my custom AngularJS filter, which should transform id to name, which will get from IndexedDb. I wrote this filter:

cardsList.filter('getCollectionName', ['IndexedDb', function(IndexedDb) {
return function(idCollection) {
    var val = 'not';
    IndexedDb.getById(IndexedDb.STORES.COLLECTION_STORE, idCollection).then(function(data) {
        val = data.name;    //here dont work return data.name;
    }, function(err){
        $window.alert(err);
    });
   return val; // here is still 'not'
};  }]);

So i have property val and i want set it with name of collection by id. But its return initial value 'not'. I knew that inside calling is not visible property val. But return data.name; inside succesful callback dont work. I looked at How do I return the response from an asynchronous call?, but I dont understood how fix my problem. How can I fix this so function return data.name from callback? Thanx

Community
  • 1
  • 1
Petr
  • 89
  • 10

1 Answers1

0

You can use $q service of the angularjs. Return the value when your promise resolves in $q.all(..)

cardsList.filter('getCollectionName', ['IndexedDb','$q', function(IndexedDb,$q) {
return function(idCollection) {
    var val = 'not';
    var promises=[];
    promises.push(IndexedDb.getById(IndexedDb.STORES.COLLECTION_STORE,idCollection).then(function(data) {
         val = data.name;    //here dont work return data.name;
         }, function(err){
           $window.alert(err);
        })
    );


    $q.all(promises).then(function(){return val;});
};  }]);
Pratik Pambhar
  • 184
  • 1
  • 7
  • Its looks well, but if I edited my code like this. Filter dont work (dont replace idCollection by return val. I needed return that value from ' this: return function(idCollection) { ' function. if I do var pom = $q.all(promises).then(... then I have in var pom: Promise object with that value, but i dont know how access to Promise->$$state->value (console log in chrome) – Petr Feb 18 '16 at 17:54
  • And Why I need $q.all()? its push to array only one promise. – Petr Feb 18 '16 at 18:10
  • It is like if you are waiting on multiple promises, you can push those to the promises array and $q.all is wait on all that promises. – Pratik Pambhar Feb 19 '16 at 04:58
  • And for your above comment, why dont try and use simple function, instead of filter. – Pratik Pambhar Feb 19 '16 at 05:01
  • Because I print data in template using ng-repeat, and for item I acces to collectionId. From that i need get name of collection. So i thought that filter is right for this situation? But I dont know how get/return value from that promise. And i dont store collection information to this item, when I need this information only on one place. – Petr Feb 19 '16 at 09:17
  • Out of all it is not advisable to make ajax request for each item in ng-repeat, it will take lot of time to load on every model change. You can pre fetch the data and store it in model and then use that data inn ng-repeat. – Pratik Pambhar Feb 19 '16 at 13:37
  • Yea, but with fetch another data to model I have similar problem. How get data from out of this function : IndexedDb.getById(IndexedDb.STORES.COLLECTION_STORE,idCollection).then(function(data) { val = data.name; //here dont work return data.name; }, – Petr Feb 19 '16 at 14:36
  • Or exist any posibbilities how work with it synchronously? – Petr Feb 19 '16 at 17:59