$resource relations in AngularJS

后端 未结 1 1439
谎友^
谎友^ 2020-12-14 04:59

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

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


        
相关标签:
1条回答
  • 2020-12-14 05:31
    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.

    0 讨论(0)
提交回复
热议问题