$资源更新方法,行为怪异 [英] $resource update method behaving strangely

查看:113
本文介绍了$资源更新方法,行为怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读/创建/删除为特定$资源的所有工作正常,但编辑是不会这么好。

Reading/Creating/Deleting is all working fine for a particular $resource, however Editing is not going so well.

在我的应用程序的配置我有:

In my app config I have:

$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

这是您在POST / PUT做工精细,也许这是值得一提。

This is working fine for both POST/PUT, maybe it's worth mentioning.

我的$资源定义如下:

app.factory('Project', function($resource) {
    return $resource('project/:id',{},{
        query: { method: 'GET', isArray: false },
        update: { method: 'PUT' }
    });
});

然后在ProjectEditCtrl:

Then in ProjectEditCtrl:

var ProjectEditCtrl = function ($scope, $routeParams, Project, $location) {
    var id = $routeParams.id;
    $scope.projectForm = Project.get({id: id});
    $scope.projectDo = function() {
        var params = $.param($scope.projectForm);
        Project.update({id: id}, params);
    }
}

现在,当我实际运行 $ scope.projectDo()我收到请求的字符串,而不仅仅是PUT,在此撷取画面所示:

Now when I actually run $scope.projectDo() I get a string of requests, not just the PUT, shown in this screencap:

为什么有任何其他呼叫比 PUT项目等/ 1 ?请注意,删除500的正在发生,因为没有:ID 中的路径

Why are there any other calls other than the PUT project/1? Note that the DELETE 500's are occurring because there's no :id in the the path.

更新

只是为了好玩,我决定改变整个$资源定义为以下内容:

Just for fun, I decided to alter the entire $resource definition to the following:

query: { method: 'GET', isArray: false },
update: { method: 'PUT' },
save: { method: 'GET' },
delete: { method: 'GET' },
get: { method: 'GET' }

测试使用这个定义证实,添加/删除控制器尊重它,并尝试使用GET为 Project.delete Project.save

运行 Project.update 现在似乎表现出较少的怪异电话。凡像以前那样有:

Running Project.update now seems to exhibit fewer weird calls. Where as before there was:


  • POST X1

  • GET X2

  • 删除X2

  • PUT X2

强制所有除了更新后()来搞定了,我现在看到的:

After forcing all except update() to GET, I now see:


  • 无POST

  • GET X2

  • 删除 X1

  • PUT X2

不过,正如困惑,但可能有用的信息。

Still just as confused, but possibly useful info.

更新2

决定尝试放弃PUT方法让 Project.update()使用POST代替,但有很多意外的请求出现类似的问题。

Decided to try abandoning the PUT method so Project.update() uses POST instead, however a similar problem occurs with lots of undesired requests.

另外请注意,所有这些请求的火在同一时间点,这意味着他们都不是连续的或反应,另一个请求的完成。

Also note, that all of these requests fire at the same point in time, meaning none of them are sequential or reacting to the completion of another request.

更新3

使用其他不同的字比更新来看看我莫名其妙地被践踏现有code试过,但 Project.whaaaat() 有同样的效果。

Tried using a different word other than update to see if I was somehow trampling on existing code, but Project.whaaaat() has the same effect.

更新4

OK,现在越来越有点接近真理。尝试没有定义的一个方法,并且会出现同样的问题。我想我只是每次射击定义的方法,当我打电话,这不是认可的方法是什么?

OK getting a bit closer to the truth now. Tried a method that was not defined, and the same behavior occurs. I suppose I'm simply firing every defined method when I call a method that's not recognized?

更新5

貌似问题是由这两条线的组合引起的:

Looks like the problem is caused by the combination of these 2 lines:

$scope.projectForm = Project.get({id: id});
var params = $.param($scope.projectForm);

在运行 $。参数呼吁所有 Project.get 返回的对象是上>项目方法,因为他们实际上是属性。

In running $.param on the object returned by Project.get it's calling all Project methods since they are actually properties.

正在运行, projectForm 属性与循环的typeof 显示:

Running a loop over projectForm properties with typeof reveals:

名称是一个字符串
id是一个数
$ get是一个函数
$保存是一个函数
$查询功能
$删除是一个函数
$删除是一个函数
$更新是一个功能

name is a string id is a number $get is a function $save is a function $query is a function $remove is a function $delete is a function $update is a function

有没有办法对一个对象,prevent运行$ .PARAM未序列化属性的序列?

Is there a way to run $.param on an object and prevent the serialization of un-serializable properties?

推荐答案

我遇到了一个同样的问题,最近使用angularjs和codeigniter。事实证明,这取决于你如何定义你的参数。

I ran into a the same problem recently using angularjs and codeigniter. It turns out that it depends on how you define your parameters.

var param1 = {'name':'some name'}; 
var param2 = { name :'some name'}; 
$http.post('some url/',$.param(param1),....); // babies die
$http.post('some url/',$.param(param2),....); // nuclear war averted

这篇关于$资源更新方法,行为怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆