Search code examples
angularjsloopbackjs

Loopback AngularJS SDK - query related models


The loopback documentation states that the AngularJS "SDK creates a Category.products() method that can be used to list all products in a given category."

$scope.products = Category.products({
    id: $scope.category.id,
});

This works fine but, how do I return a list of all categories with all related products. I have been through the documentation and the source core (lb-services.js) and can't see how to do this.

Any ideas?


Solution

  • Worked it out. You need to create a remote method in the parent model (ensure you've set up the relationships between the models).

    Supplier.supplierServices = function (cb) {
        var response;
        Supplier.find({include: 'Services'}, function (err, suppliers) {
            console.log('err', err, 'suppliers', suppliers);
            response = suppliers;
            cb(null, response);
        });
    };
    

    Then run lb-ng to expose the api to the angular SDK. You can then call the remote method from the client.