我的POST和PUT返回服务器上看到的当前实体状态.如何在服务器调用后告诉Restangular更新其模型对象?
以防万一 - 为什么?因为在保存/更新期间,服务器可能已经计算了一些字段.因为其他客户端可能已同时更新,我想确保当前正在更新的客户端是最新的.
到目前为止我只发现了这个:
var res = {foo: "bar"};
var cb = function(response) {
$.extend(res, response);
}
// POST / create
Restangular.all("resource").post(res).then(cb);
// PUT / update
res.put().then(cb);
Run Code Online (Sandbox Code Playgroud)
在我怀疑必须有更好的方法的同时,它看起来如此肮脏和普遍.
编辑
上面的代码只演示了两个不同的操作.我的目标是在创建实体后不向服务器发送更新,而是在客户端更新状态.如果愿意,可以将状态与服务器同步.
当我发布一个对象:
var res = {foo: "bar"};
Restangular.all("resource").post(res);
Run Code Online (Sandbox Code Playgroud)
服务器响应具有以下内容:
{"foo": "bar", "id": 15}
Run Code Online (Sandbox Code Playgroud)
...然后我想Restangular res自动更新和重新处理对象,所以它有ID,可以put()等.
现在是一个不同/互补的场景.当我PUT一个Restangular对象时:
var res = Restangular.one("resource", 15).get();
// returns {foo: "bar", id: 15}
res.put();
Run Code Online (Sandbox Code Playgroud)
服务器始终回复对象的当前状态,可能是:
{"foo": "buzz", "id": 15}
Run Code Online (Sandbox Code Playgroud)
...所以我希望Restangular res在此PUT之后自动更新.
我喜欢Restangular for AngularJS的所有功能,除了我找不到(并且它可能不支持)一种只传递完整URL的方法.我意识到.one('something','someparam')的好处,但我的问题是我传递了各种URL字符串,我真的不想分裂()它们只是为了使用Restangular.
另外,我知道baseURL函数; 但是我传递的这些URL并不一定来自相同的基本路径.
例如,我可能有:
/us/en/product/17726
/us/es/products
/us/es/product/991A0
/ca/en/accounts
Run Code Online (Sandbox Code Playgroud)
我给的只是弦乐......
我似乎无法弄明白这一点 -
我想做的事情如下:
content = Restangular.one('contents', 2).get()
content.name = "Chunky Bacon"
content.put()
Run Code Online (Sandbox Code Playgroud)
但这不起作用 - content没有put功能,有趣的是,它没有一个name(是因为它是一个"承诺"?)
现在,Restangular文档确实描述了更新对象,但是您将该对象作为集合的一部分:
content_id2 = Restangular.all("contents").getList()[2]
content_id2.name = "Chunky Bacon"
content_id2.put()
Run Code Online (Sandbox Code Playgroud)
这很有效.但!我不仅获取了整个列表,我还无法弄清楚如何通过它的ID轻松获取我的内容; 就像是Restangular.all("contents").getList().find(2)
customPUT(..) 似乎是下一个最好的选择:
Restangular.one("contents", 2).customPUT("", {"content" : {"name" : "Chunky Bacon"} } )
Run Code Online (Sandbox Code Playgroud)
只有当Rails服务器收到它时,params["content"] == {id: 2}- 似乎客户端的东西正在踩那个值.(再次注意,没有name字段).现在,我可以通过以下方式解决这个问题:
Restangular.one("contents", 2).customPUT("", {"content_data" : {"name" : "Chunky Bacon"} )
Run Code Online (Sandbox Code Playgroud)
但看起来我真的错过了你应该如何做到这一点.
所以看起来在示例中你可以这样做:
App.controller('ProjectListCtrl', ['$scope', 'Restangular', function($scope, Restangular) {
$scope.projects = Restangular.all('project/').getList();
}]);
Run Code Online (Sandbox Code Playgroud)
但那对我不起作用.当我在项目中重复项目并查看范围时,我看到:
{
projects: {
then: null
catch: null
finally: null
call: null
get: null
restangularCollection: true
push: null
}
}
Run Code Online (Sandbox Code Playgroud)
哪个看起来像一个未解决的承诺对象?
这工作正常,但更详细:
lgtApp.controller('ProjectListCtrl', ['$scope', 'Restangular', function($scope, Restangular) {
Restangular.all('project/').getList().then(function(projects){
$scope.projects = projects;
});
}]);
Run Code Online (Sandbox Code Playgroud)
我错过了什么?这是在文档中:
$scope.owners = house.getList('owners')
Run Code Online (Sandbox Code Playgroud)
无所谓,但是当我在Ripple chrome插件中测试一个phonegap应用时会发生这种情况.
我正在使用Django REST框架用于API和Angular SPA与Restangular用于通信API.有时我必须添加多个对象才能添加,我认为我可以将它们一起发送到数组中并发出一个请求.
当我尝试从Restangular中执行此操作时,我错误地输入了错误输入.如果我尝试从REST框架Web界面添加多个对象,我将传递对象或对象数组:
// this { "text": "gdhg", },{ "text": "gdhg", },{ "text": "gdhg", }
// or this [{ "text": "gdhg", },{ "text": "gdhg", },{ "text": "gdhg", }]
Run Code Online (Sandbox Code Playgroud)
但我收到了ParseError.我错了,我必须改变或如何正确地做.
是否可以使用带有嵌套路由的extendModel或extendCollection?
例如,
http://myserver.com/topresources/nestedresources
Restangular.extendModel('topresources/nestedresources',function(model){ ... })
有一个github问题,它Restangular.setOnElemRestangularized通过手动处理嵌套路由引用了嵌套资源...但我宁愿坚持使用普通的extendModel/extendCollection方法.
由于某种原因,这在IE中比Chrome/FF更容易:
$scope.download = function() {
Restangular.one(myAPI)
.withHttpConfig({responseType: 'blob'}).customGET().then(function(response) {
//IE10 opens save/open dialog with filename.zip
window.navigator.msSaveOrOpenBlob(response, 'filename.zip');
//Chrome/FF downloads a file with random name
var url = (window.URL || window.webkitURL).createObjectURL(response);
window.location.href = url;
});
};
Run Code Online (Sandbox Code Playgroud)
有没有办法做类似于IE10 +的工作方式?也就是说,我可以指定一个文件名/类型(只会是zip)?
是否存在角度服务将缓存Restangular/$ http调用而未明确告知这样做的情况?例如,我有一个服务做这样的事情:
function getSomeThings(){
return Restangular.one('things').get().then(function (thing) {
return thing;
});
}
Run Code Online (Sandbox Code Playgroud)
每次页面刷新时都会调用此服务(它在UI路由器路由解析中).是否有可能每次都不会进行此调用,但Angular会以某种方式缓存这些调用,而没有明确告知这样做?
我熟悉如此明确的缓存:
RestangularProvider.setDefaultHttpFields({cache: true});
Run Code Online (Sandbox Code Playgroud)
这不是意图.我的问题是角度服务是否具有一些固有的缓存逻辑,如果是,则如何覆盖它.
我在我的项目中使用Restangular,之前这段代码适用于检索对象数组:
var params = {name: "Stack", surname: "Overflow"}
var service = Restangular.all('users')
service.getList(params)
Run Code Online (Sandbox Code Playgroud)
来自服务器的响应只是一个对象数组:
[
{...},
{...}
]
Run Code Online (Sandbox Code Playgroud)
但现在我添加了分页,我的响应现在不包含数组,而是包含数组的对象:
{
totalCount: 500,
data: [
{...},
{...}
]
}
Run Code Online (Sandbox Code Playgroud)
此外,我改service.getList(params)到service.get(params)(因为getList预计只有阵列).
在此更改后,我的GET参数没有字符串化,即我在调试器请求中看到如下:
users/[object%20Object]
但是早些时候(当使用getList方法时)它按预期工作:
users?name=Stack&surname=Overflow
这里有什么问题?
我正在尝试使用restangular进行文件上传请求,我想在restangular中实现与下面相同的功能.但是,我不确定如何为此特定请求设置内容类型和transformRequest.如果我理解正确,setDefaultHeader会为所有后续请求设置它.还有其他方法吗?
myApp.service('$fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var filedata = new FormData();
filedata.append('file', file);
$http.post(uploadUrl, filedata, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
}
}]);
Run Code Online (Sandbox Code Playgroud) restangular ×10
angularjs ×9
javascript ×3
rest ×2
django ×1
file-upload ×1
http ×1
post ×1
put ×1