【问题标题】:API returning 3 records - backbone collection not matchingAPI 返回 3 条记录 - 主干集合不匹配
【发布时间】:2014-10-15 01:18:36
【问题描述】:

我有一个返回“Groups”的JSON对象的api,PHP如下,

public function index()
{
    $teams = new Team;
    $clients = new Client;
    $organisations = new Organisation;

    //$org = Organisation::find(1);
    //return json_encode($org->clients());

    // Get the organisations
    $org = Organisation::all()->toArray();

    $organisations = array();

    foreach( $org as $k => $v ) {
        $organisations[$k]['id'] = $v['id'];
        $organisations[$k]['name'] = $v['name'];
        $organisations[$k]['information'] = $v['information'];
        $organisations[$k]['notifications'] = $v['notifications'];
        $organisations[$k]['add_all'] = $v['add_all'];
        $organisations[$k]['created_at'] = $v['created_at'];
        $organisations[$k]['updated_at'] = $v['updated_at'];
        $organisations[$k]['type'] = $v['type'];
        $organisations[$k]['clients'] = Organisation::find($v['id'])->clients;
        $organisations[$k]['projects'] = Organisation::find($v['id'])->projects;
        $organisations[$k]['members'] = Organisation::find($v['id'])->users;
        $organisations[$k]['teams'] = Organisation::find($v['id'])->teams;
    }

    // Get the clients
    $cli = Client::all()->toArray();

    $clients = array();

    foreach( $cli as $k => $v) {
        $clients[$k]['id'] = $v['id'];
        $clients[$k]['name'] = $v['name'];
        $clients[$k]['information'] = $v['information'];
        $clients[$k]['add_all'] = $v['add_all'];
        $clients[$k]['created_at'] = $v['created_at'];
        $clients[$k]['updated_at'] = $v['updated_at'];
        $clients[$k]['type'] = $v['type']; 
        $clients[$k]['members'] = Client::find($v['id'])->users;
    }


    // Get the teams
    $team = Team::all()->toArray();

    $teams = array();

    foreach( $team as $k => $v ) {
        $teams[$k]['id'] = $v['id'];
        $teams[$k]['name'] = $v['name'];
        $teams[$k]['information'] = $v['information'];
        $teams[$k]['created_at'] = $v['created_at'];
        $teams[$k]['updated_at'] = $v['updated_at'];
        $teams[$k]['type'] = $v['type']; 
        $teams[$k]['members'] = Team::find($v['id'])->users;
    }

    $result = array_merge($organisations, $clients, $teams);

    return Response::json($result, 200);

}

这将返回以下 JSON 对象,

[
{
    "id": "1",
    "name": "Organisation",
    "information": "This is some information about the organisation. ",
    "notifications": "0",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "organisation",
    "clients": [],
    "projects": [],
    "members": [],
    "teams": []
},
{
    "id": "1",
    "name": "Client",
    "information": "",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "client",
    "members": []
},
{
    "id": "2",
    "name": "Developers",
    "information": "",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "team",
    "members": []
}

]

我有一个使用此数据的组集合(通过 API 填充),集合文件如下所示,

  var GroupCollection = Backbone.Collection.extend({

    url: '/groups/get',
    model: app.Group,

    initialize: function() {

    }

});

在我看来,我会做以下事情,

初始化:函数(){

    var that = this;

    this.filteredCollection = new app.userFilteredCollection;

    this.$el.find("h4").text("Edit " + this.model.get('name'));

    this.$el.attr('id', 'editProject');

    this.$el.find(".modal-body").html( this.template(this.model.toJSON() ) );

    this.groupsCollection = new GroupCollection;
    this.groupsCollection.fetch();

    this.render();

},

这会触发对组 API 端点的 GET 请求。但是如果我在邮递员之类的东西中运行端点,该集合只有 2 条记录而不是 3 条记录。

为什么我会得到不同的结果?

【问题讨论】:

    标签: javascript php api backbone.js laravel


    【解决方案1】:

    您有 2 个具有相同 id 值的模型。集合中的每个模型都必须有一个唯一的id

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多