在ui-router自定义路径中编辑Restangular模型时,PUT url中的ID重复

时间:2015-09-15 07:28:59

标签: angular-ui-router restangular

目前我正在设置restangular来编辑我的模型。以前我没有使用自定义网址来显示表单,比如

http://example.com/admin/clients (I just load the form there).

但是,我使用ui-router来设置这样的URL

http://example.com/admin/clients/{clientId}/edit

当我执行model.put()model.save()时,生成的PUT网址错误。它变得像这样

PUT http://referral.dev/admin/clients/f2aa1490-5a8f-11e5-b936-b3a54e0c9925/f2aa1490-5a8f-11e5-b936-b3a54e0c9925

应该是这样的:

PUT http://referral.dev/admin/clients/f2aa1490-5a8f-11e5-b936-b3a54e0c9925

我做错了什么?

为方便起见,这里是来自restangular对象的调试:

{
    "restangularCollection": false,
    "$object": {
        "id": "f2aa1490-5a8f-11e5-b936-b3a54e0c9925",
        "referrer_id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
        "email": "Claudia.Kertzmann@hotmail.com",
        "first_name": "Isabelle",
        "last_name": "Herzog",
        "mobile": "450.357.8579x621",
        "stage": "member",
        "stage_at": "2015-09-09 00:00:00",
        "previous_stages": [
            {
                "stage": "lead",
                "stage_at": "2015-08-30 00:00:00"
            }
        ],
        "total_investment": 190000,
        "email_token": "caf34dddfe00f36b180d459236830934",
        "confirmed": false,
        "unsubscribed": false,
        "transfered_at": null,
        "previous_referrers": [],
        "created_at": "2015-09-14 03:23:20",
        "updated_at": "2015-09-14 03:23:20",
        "referrer": {
            "id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
            "email": "Eulalia.Pacocha@Dickens.biz",
            "name": "Esperanza Aufderhar Sr.",
            "mobile": "658.549.0277",
            "role": "referrer",
            "organization_id": "f0577b10-5a8f-11e5-984f-f120de15e3c1",
            "last_login_at": "0000-00-00 00:00:00",
            "suspended": 0
        },
        "route": "f2aa1490-5a8f-11e5-b936-b3a54e0c9925",
        "reqParams": null,
        "restangularized": true,
        "fromServer": true,
        "parentResource": {
            "route": "admin/clients",
            "parentResource": null
        },
        "restangularCollection": false
    }
}

而且,如果不使用自定义URL(工作正常),这里是一个重要的对象:

{
    "id": "f2aa1490-5a8f-11e5-b936-b3a54e0c9925",
    "referrer_id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
    "email": "Claudia.Kertzmann@hotmail.com",
    "first_name": "Isabelle",
    "last_name": "Herzog",
    "mobile": "450.357.8579x621",
    "stage": "member",
    "stage_at": "2015-09-09 00:00:00",
    "previous_stages": [
        {
            "stage": "lead",
            "stage_at": "2015-08-30 00:00:00"
        }
    ],
    "total_investment": 190000,
    "email_token": "caf34dddfe00f36b180d459236830934",
    "confirmed": false,
    "unsubscribed": false,
    "transfered_at": null,
    "previous_referrers": [],
    "created_at": "2015-09-14 03:23:20",
    "updated_at": "2015-09-14 03:23:20",
    "referrer": {
        "id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
        "email": "Eulalia.Pacocha@Dickens.biz",
        "name": "Esperanza Aufderhar Sr.",
        "mobile": "658.549.0277",
        "role": "referrer",
        "organization_id": "f0577b10-5a8f-11e5-984f-f120de15e3c1",
        "last_login_at": "0000-00-00 00:00:00",
        "suspended": 0
    },
    "route": "admin/clients",
    "reqParams": null,
    "restangularized": true,
    "fromServer": true,
    "parentResource": null,
    "restangularCollection": false
}

我已尝试手动将$object.route设置为" admin / clients"但PUT网址仍然相同。

谢谢.. :))

1 个答案:

答案 0 :(得分:0)

我已经解决了。我以前用过:

var models = Restangular.all('admin/clients');

然后使用

models.one($stateParams.clientId).get()

获得模型。

正确的方法是使用Restangular.one加载模型,例如。像这样:

vm.model = Restangular.one('admin/clients', $stateParams.clientId).get();