angularjs - use the $q promise like the promise returned from $resource -


with angular's $resource promise able assign promise right variable , use out having assign results of promise varible in success function. wondering if can achieve same thing $q?

example

.service("rest", function($resource){         return {             user: $resource( "http://some.com/url")         }     })     .controller("myctrl", function($scope, rest){         $scope.user = rest.user.get(); <-- want able $q     }) 

short answer: no

the $resource interface designed allow kind of 'futures' behaviour. when call .get() returns placeholder object filled data when it's available.

because $resource internal angular factory, knows when trigger digest cycles makes seem can use $scope.user beginning. in actuality, won't populated data until there has been http response server.

a promise on other hand, general purpose mechanism. doesn't know kind of placeholder object store it's values in when arrive. doesn't kind of values arrive either. promises expose series of callback methods can use let know when promise ready.


long answer: sort of (you can use promises build interfaces work this)

this kind of mechanism works because of way angular dirty checking on scopes. when asynchronous happens, needs let angular know should checking see whether has changed , needs update it's views.

when data arrives our http response $resource factory responsible letting angular know. means $scope.user object show in view arrives. $resource responsible making stuff happen when async event occurs. in case, async event http response arriving data.

when using promises, responsible making stuff happen when async event occurs. promises specific angular, instead promises have generic interface dealing different async events can happen promise. it's job work when 1 of them happens, why short answer no.

however, if implementing factory/service/provider, behaviour desirable, possible.

let's make rocket factory creates instances of rockets, have parts added later, in same way $resource does, using $q factory.

app.factory('rocket', function(junkyard) {   return function(name) {     var rocket = {};      rocket.name = name || 'boring rocket';     rocket.parts = [];     rocket.promise = junkyard.getparts();      // junkyard.getparts returns $q promise     // resolved object     // of things can attached rocket.     //     // might     // [     //   { name: "thrusters", "quantity":  3 },     //   { name: "engines", "quantity": 100 },     //   { name: "robots", "quantity": 2 }     // ]      rocket.promise.then(function(parts) {       parts.foreach(function(part) {         rocket.parts.push(part);       });     });      return rocket;   }; }); 

now can use our new factory in similar kind of way $resource:

function mycontroller($scope, rocket) {   $scope.rocket = rocket('top rocket');   // can use $scope.rocket here!    $scope.rocket.name = 'even better rocket';   // won't have properties   // junkyard.getparts yet. } 

and can template these values straight our views.

<section ng-controller='mycontroller'>   <h1 ng-bind='rocket.name'></h1>   <table>     <th>       <td>name</td>     </th>     <th>description</th>     <tr ng-repeat='part in rocket.parts'>       <td ng-bind='part.name'></td>       <td ng-bind='part.description'></td>     </tr>   </table> </section> 

as promise resolves, properties added rocket , scope digest triggered $q, because $q knows async event occurred.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -