javascript 如何在restangular中使用PUT方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27587179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use PUT method in restangular
提问by Binson Eldhose
I am using restangular, but I have a problem with "Put" method, its not working as expected
我正在使用 retangular,但“Put”方法有问题,它没有按预期工作
My angularService code
我的 angularService 代码
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
但是我收到一个错误“Put”不是一个函数,我检查了用户对象,发现用户对象没有重新定义(没有找到任何其他方法)。我该如何解决
回答by Paul Smith
You can only 'put' on a data object.
您只能“放置”数据对象。
customPUT([elem, path, params, headers])
is what you want. use it like this:
customPUT([elem, path, params, headers])
是你想要的。像这样使用它:
Restangular.all('yourTargetInSetPath').customPUT({'something': 'hello'}).then(
function(data) { /** do something **/ },
function(error) { /** do some other thing **/ }
);
回答by dhavalcengg
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.
您只能在重新定义的对象中使用 put 方法。要触发任何对象,您需要检查 put 方法,如果对象不包含任何 put 方法,那么您需要将该对象转换为重新定义的对象。
Change your updateUser to following :
将您的 updateUser 更改为以下内容:
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.
Restangular.copy 方法会在对象中添加一些额外的 retangular 方法。简而言之,它会将任何对象转换为重新定义的对象。