mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

extendModel not working as I'd expect #1101

Open uberspeck opened 9 years ago

uberspeck commented 9 years ago

I'm trying to create a basic data model but methods applied via extendModel are not being recognized...

  myApp.service( "User", ($log, Restangular) ->
    #
    # Collection methods
    Restangular.extendCollection("users", (obj) ->
      angular.extend(obj,
        get: (id) ->
          @one(id).get()
      )
    )
    #
    # Record methods
    Restangular.extendModel("users", (obj) ->
      angular.extend(obj,
        talk: ->
          alert "hi"
      )
    )
    Restangular.all("users")
  )

...then in my controller:

  myApp.controller( "EditUserCtrl", ($stateParams, User) ->

    vm = @

    User.get($stateParams.userId).then( (response) ->
      vm.record = response
      vm.record.talk() # <= undefined function error???
    )
  )
lstone commented 9 years ago

+1

blaues0cke commented 9 years ago

+1

To be more concrete, in my case I have the following issue:

... and as I wrote this comment, I figured out that this is working:

    api.get(1).then(function(data)
    {
        console.log(data.getDropDownValue());
    });
sava-vidakovic commented 9 years ago

+1

daviesgeek commented 8 years ago

I think the problem is when you define your service. Try something like this:

http://plnkr.co/edit/hA8Ft3P1Kjc6jqWSZktg?p=preview

daviesgeek commented 8 years ago
 .service("User", function($log, Restangular) {
   var User = Restangular.all("users");

  Restangular.extendCollection("users", function(obj) {
    return angular.extend(obj, {
      get: function(id) {
        return this.one(id).get();
      }
    });
  });
  Restangular.extendModel("users", function(obj) {
    return angular.extend(obj, {
      talk: function() {
        return alert("hi");
      }
    });
  });

  return User
}