Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$resource relations in AngularJS

The usual way of defining an isolated resource in AngularJS is:

angular.service('TheService', function($resource){
  return $resource('api/url');
});

I'm trying to figure out the best way to write a model that relates to other models, such as an Order that has 1 or more OrderItems. My first idea is this:

  1. Create the OrderService and OrderItemService as independent resource models
  2. Write a controller that queries the OrderService and watches the result array
  3. When the result array changes, query the OrderItemService for all of the item IDs and decorate the order object with extended information as it comes in

That seems a bit messy. Is there a more elegant way?

like image 755
Ben Straub Avatar asked Apr 02 '12 17:04

Ben Straub


People also ask

What is $resource in AngularJS?

Overview. A factory which creates a resource object that lets you interact with RESTful server-side data sources. The returned resource object has action methods which provide high-level behaviors without the need to interact with the low level $http service. Requires the ngResource module to be installed.

What is the difference between factory and service in AngularJS?

The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not. That is why, in the case of a factory, we return an object literal instead of using this.

What is debounce in AngularJS?

debounce : integer value which contains the debounce model update value in milliseconds. A value of 0 triggers an immediate update. If an object is supplied instead, you can specify a custom value for each event. For example: ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 500, 'blur': 0 } }"

Which directive is used for non immediate model updates?

In AngularJS, immediate model updates is a default behavior but if we want to persist value in the object after a certain time period has elapsed then we can achieve the same by using “debounce” object in “ng-model-options” directive, which is known as “non-immediate model updates”.


1 Answers

angular.service('OrderItem', function($resource) {
  return $resource('api/url/orderItem');
});

angular.service('Order', function($resource, OrderItem) {
  var Order = $resource('api/url/order');

  Order.prototype.items = function(callback) {
    return order.query({orderId: this.id}, callback);
  }
  return Order
});

Would something like above solve your problem? You would then use it as

var order, items;

Order.get({id: 123}, function(o) {
  order = o;
  o.items(function(is) { items = is; });
});

Angular's $resource does not understand relationships. It is something we would like to change in post 1.0.

I don't think you should put the data on the order directly, since it is not part of it, and you will have issues persisting the order since it will now have the items object as well.

like image 106
Misko Hevery Avatar answered Sep 21 '22 18:09

Misko Hevery