Backbone - 更改默认的GET URL格式

时间:2016-01-01 10:57:10

标签: javascript json rest backbone.js

REST:

我的REST API位于以下网址: http://localhost/Project/index.php/rest/resource/car

这将运行标准的get请求,该请求将表中的所有数据作为JSON返回。 但是,我的REST API也有以下内容:

http://localhost/Project/index.php/rest/resource/car/carId/2

使用时只返回Car的数据,carId = 2。

骨干:

    this.mycar.fetch({
        data: { 'carId': '1' },
        success: function () { ...

在骨干网中运行上述代码将创建并运行如下所示的请求:http://localhost:7070/Project/index.php/rest/resource/car?carId=1,这与我的REST api不兼容,那么如何更改它?

编辑: 模型和收藏:

var Car = Backbone.Model.extend({
    urlRoot: ROOT + 'car',
    idAttribute: 'carId'

});

var Cars = Backbone.Collection.extend({
    model: Car,
    url: ROOT + 'car',
})

2 个答案:

答案 0 :(得分:1)

data属性是jQuery $.ajax的{​​{1}}属性。对于data请求,它用于设置查询参数。

您应该更新模型的.url方法/属性。

直接将其设置为参数:

GET

修改:我刚注意到您的网址中有this.mycar.url = 'api_entry_point/car/' + car_id; 作为资源。这是一个非常传统的网址。看来这就是你的模型构造函数中有carId的原因。 idAttribute: 'carId'不必在网址中。它可能是:

carId

在上面的网址中,http://localhost/Project/index.php/rest/resource/car/2 是模型ID。网址中绝对不需要2。此外,由于资源包含多个项目,因此使用carId代替cars段更有意义。

car

当然,这只是个人偏好。

答案 1 :(得分:1)

在您的骨干模型文件中,您可以像这样传递

List<string> finalLines = richTextBox1.Lines.ToList();
finalLines.RemoveAll(x => x.StartsWith("2016") && x.EndsWith("message\":\""));
richTextBox1.Lines = finalLines.ToArray();
你可以在骨干视图中传递

url : return "http://localhost/Project/index.php/rest/resource/car/carId/=" + this.id;
相关问题