最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use PUT method in restangular - Stack Overflow

programmeradmin12浏览0评论

I am using restangular, but I have a problem with "Put" method, its not working as expected

My angularService code

 var userService = function (restangular) {
            var resourceBase = restangular.all("account/");

            restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
                if (operation == "getList") {
                    return response.data;
                }
                return response;
            });
      this.getUserById = function (id) {

                return resourceBase.get(id);
                // return restangular.one("account", id).get();
            };
            this.updateUser = function(user) {
                return user.Put();
            };
}

My controller code

 var userEditController = function (scope, userService, feedBackFactory, $routeParams) {

        scope.user = undefined;

        scope.updateUser = function () {

            userService.updateUser(scope.user).then(function (data) {
                feedBackFactory.showFeedBack(data);
            }, function (err) {
                feedBackFactory.showFeedBack(err);
            });
        };

        userService.getUserById($routeParams.id).then(function (data) {
            scope.user = data.data;   **// Please not here I am reading the object using service and this object is getting updated and pass again to the service for updating** 

        }, function (er) {

            feedBackFactory.showFeedBack(er);
        });

    };

but I am getting an error "Put" is not a function , I checked the user object and I found that the user object is not restangularized ( no any additional methods found). How can I solve it

I am using restangular, but I have a problem with "Put" method, its not working as expected

My angularService code

 var userService = function (restangular) {
            var resourceBase = restangular.all("account/");

            restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
                if (operation == "getList") {
                    return response.data;
                }
                return response;
            });
      this.getUserById = function (id) {

                return resourceBase.get(id);
                // return restangular.one("account", id).get();
            };
            this.updateUser = function(user) {
                return user.Put();
            };
}

My controller code

 var userEditController = function (scope, userService, feedBackFactory, $routeParams) {

        scope.user = undefined;

        scope.updateUser = function () {

            userService.updateUser(scope.user).then(function (data) {
                feedBackFactory.showFeedBack(data);
            }, function (err) {
                feedBackFactory.showFeedBack(err);
            });
        };

        userService.getUserById($routeParams.id).then(function (data) {
            scope.user = data.data;   **// Please not here I am reading the object using service and this object is getting updated and pass again to the service for updating** 

        }, function (er) {

            feedBackFactory.showFeedBack(er);
        });

    };

but I am getting an error "Put" is not a function , I checked the user object and I found that the user object is not restangularized ( no any additional methods found). How can I solve it

Share Improve this question asked Dec 21, 2014 at 6:16 Binson EldhoseBinson Eldhose 1,0134 gold badges15 silver badges36 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

You can only 'put' on a data object.

customPUT([elem, path, params, headers]) is what you want. use it like this:

Restangular.all('yourTargetInSetPath').customPUT({'something': 'hello'}).then(
  function(data) { /** do something **/ },
  function(error) {  /** do some other thing **/ }
);

You can have put method only in restangularized objects. To fire put on any object, you need to check for put method, if object does not contain any put method then you need to convert that object in restangularized object.

Change your updateUser to following :

 this.updateUser = function(user) {
    if(user.put){
        return user.put();
    } else {
        // you need to convert you object into restangular object
        var remoteItem = Restangular.copy(user);

        // now you can put on remoteItem
        return remoteItem.put();
    }
 };

Restangular.copy method will add some extra restangular methods into object. In short, it will convert any object into a restangularized object.

发布评论

评论列表(0)

  1. 暂无评论