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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With