简体   繁体   English

收集-JSON响应的模型用法?

[英]Collection - Model usage for JSON response?

Regarding the pattern Backbone uses for Collections and Models , I am not sure if what I am trying to achieve is possible. 关于Backbone用于Collections和Models的模式,我不确定我试图实现的目标是否可行。

I am wanting the Collection to act as a constructor by making a AJAX POST request to fetch JSON. 我希望集合通过发出AJAX POST请求以获取JSON来充当构造函数。 Using that JSON response it will instantiate multiple models and add them to an array. 使用该JSON响应,它将实例化多个模型并将其添加到数组中。

Each object has the attributes which will be stored in my model eg 每个对象都有将存储在我的模型中的属性,例如

    define([
    'underscore',
    'backbone'
], function (_, Backbone)
{
    'use strict';

    var Employee= Backbone.Model.extend({

        defaults: {
            name: '',
            skill: '',
            latitude : 0,
            longitude : 0
        },

    });

    return Employee;
});

JSON Response JSON回应

[
{
    "name" : "bob",
    "skill" : "project manager",
    "latitude" : 12512.25,
    "longitude" : 95952.26
},


{
    "name" : "sarah",
    "skill" : "software dev",
    "latitude" : 89432.25,
    "longitude" : 1205.26
},


{
    "name" : "tom",
    "skill" : "evil sys admin",
    "latitude" : 1215,
    "longitude" : 92325
}

] ]

Collection 采集

    define([
    'underscore',
    'backbone',
    'models/employee'
], function (_, Backbone, Store, Employee) {
    'use strict';

    var Employees = Backbone.Collection.extend({
        // Reference to this collection's model.
        model: Employee,

    });

    return new Employees();
});

Code

emps = new Employees();
emps.url("/testURL"); //
emps.sync();

emps.model[0]; //undefined !!!

So from that I can conclude that the Collection is not smart enough to instantiate an array of Employee models from the JSON response. 因此,我可以得出结论,集合不够聪明,无法从JSON响应实例化Employee模型的数组。 How can I do this? 我怎样才能做到这一点?

The function you're looking for is fetch . 您正在寻找的功能是fetch Fetch uses sync to fetch data, and then instantiates models accordingly. 提取使用sync来提取数据,然后相应地实例化模型。

If your API doesn't respond to a GET /someurl then what you need to override is the sync method. 如果您的API不响应GET /someurl那么您需要重写的是sync方法。 Read the source to see how it works. 阅读源代码以了解其工作原理。

Also, you're not using url properly. 另外,您没有正确使用网址。 It should be a string, or a function that returns a string. 它应该是一个字符串,或者是一个返回字符串的函数。

var employees = new Employees();
employees.url = '/my/testurl';
// note that fetch is async
employees.fetch().done(function () {
  console.log(employees.length);
});

Right now your collection constructor code is getting undefined as its input array, and thus remains empty. 现在,您的集合构造函数代码undefined为其输入数组,因此保持为空。

You want to pass your collection into your Employees constructor, like 您想将集合传递给Employees构造函数,例如

var emps = new Employees(arrayOfEmployees, {model: Employee});

Also, that last part (re-specifying the model) is likely not necessary. 另外,最后一部分(重新指定模型)可能不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM