$resource relations in Angular.js [updated]

后端 未结 2 840
难免孤独
难免孤独 2021-01-06 08:20

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.

Ho

相关标签:
2条回答
  • 2021-01-06 08:29

    You could wrap the function in another that gets order items?

    myApp.factory('Order', function($resource) {
      var res = $resource('/api/Order/:orderId', {}, {
        '_get': { method: 'GET' }
      });
    
      res.get = function(params, success, error) {
        return res._get(params, function(data) { 
          doOrderItemStuff();
          success(data);
        }, error);
      }
      return res;
    }
    
    0 讨论(0)
  • 2021-01-06 08:31

    Before Andy's answer, I was workarounding it in Controllers. To do this, just add:

    function OrderCtrl($scope, $routeParams, $resource, Order, OrderItem) {
    
        $scope.order = Order.get({
            orderId : $routeParams.orderId
        }, function(order) {
                    doOrderItemStuff();
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题