Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested decoupled service: how to? #1381

Open
destegabry opened this issue May 31, 2016 · 3 comments
Open

Nested decoupled service: how to? #1381

destegabry opened this issue May 31, 2016 · 3 comments

Comments

@destegabry
Copy link

destegabry commented May 31, 2016

I'm trying to follow the documentation and after reading #139 and #1254 I'm more confused than ever.
I have some tables on my DB, let's say User, Customer and UserCustomer (which implements an n:n relation between User and Customer).
I have set up endpoints in my server at /user/[id], /customer/[id] and /user/[iduser]/customer/[idcustomer]
I can CRUD on User and Customer tables using the Restangular.service syntax:

angular.module('...')
  .factory('User', function (Restangular) {
    return Restangular.service('user');
  })
  .factory('Customer', function (Restangular) {
    return Restangular.service('customer');
  })

In my User factory I would like to define a getCustomer method which populates an user instance with a customers variable, pointing to a collection of Customer.
Seems I can do something like:

angular.module('...')
  .factory('User', function (Restangular) {
    Restangular.extendModel('user', function(model) {
      if (model.customers) {
        Restangular.restangularizeCollection(null, model.customers, 'customer');
      }
      return model;
    });
    return Restangular.service('user');
  });

but can't figure out how to use it in a controller!
I'd like to do something like:

angular.module('...')
  .controller('...', function ($scope, User) {
    User.getList().then(function (users) {
      $scope.users = users;
      angular.forEach(users, function (user) {
        user.customers.getList();
      });
    });
    $scope.addUserCustomerAssociation = function (user, customer) {
       user.customers.post(customer.id);
    };
   $scope.removeUserCustomerAssociation = function (user, customer) {
       user.customers.remove(customer.id);
    };
  });
@daviesgeek
Copy link
Collaborator

You should be able to do that in a controller. What isn't working?

@alexjose
Copy link

alexjose commented Dec 9, 2016

I am also trying to figure this out. How to build Nested decoupled services?

@ashgibson
Copy link

That should be working, the only thing to note is that because you are passing 'null' in a the first parameter to restangularizeCollection, it won't build a nested url for you e.g. '/users/1/customers'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants