简体   繁体   English

使用骨架.js获取集合,但未设置每个模型的ID

[英]Fetching collection with backbone.js but id for each model is not set

In short, I'm fetching a collection but afterwards I'm unable to get a specific entity from the collection using either its id or cid . 简而言之,我正在获取一个集合,但是此后,我无法使用其idcid从该集合中获取特定实体。

For this problem, consider my two REST endpoints /sites and /sites/some-site-id/entities Both sites and entities are collections. 对于此问题,请考虑我的两个REST端点/sites/sites/some-site-id/entities这两个sitesentities都是集合。 Here are my models/collections: 这是我的模型/收藏:

Models / collections 型号/收藏

var ROOT = 'http://localhost:5000';

Entity = Backbone.Model.extend({
    idAttribute: "_id",
    defaults: {
        xpos: 10,
        ypos: 10,
        site: "default"
    }
});

Entities = Backbone.Collection.extend({
    model: Entity,
    ownUrl: '/entities',
    site: {},
    url: function() {
        return ROOT + "/sites/" + (this.site? this.site : 'null') + this.ownUrl;
    },
    initialize: function(models, options) {
        if(!options.site) throw new Error("No site associated with entities");
        if(!options.site.get("_id")) throw new Error("Associated site has no _id");

        this.site = options.site.get("_id");
    }
});

Site = Backbone.Model.extend({
    idAttribute: "_id",
    defaults: {
        name: "default",
        description: "My site"
    }
});

Sites = Backbone.Collection.extend({
    model: Site,
    url: ROOT + "/sites"
});

My problem is that when I'm fetching the entities for a site, I cannot lookup a certain entity from the collection using the get method on collection. 我的问题是,当我为网站获取实体时,无法使用集合上的get方法从集合中查找某个实体。 I simply get undefined returned. 我只是得到undefined返回。

This is how I test it (entities.fetch will get 4 entities): 这就是我测试的方式(entities.fetch将获得4个实体):

Test code 测试码

var site1 = new Site({id : "52813a2888c84c9953000001"});
sites.add(site1);
site1.fetch({success: function(model, response, options) {
    entities = new Entities([], {site: site1});
    entities.fetch({success: function(model, response, options) {
        entities.each(function (entity) {
            console.log("entity.get(\"_id\") = " + entity.get("_id"));
            console.log("entity.id = " + entity.id);
            console.log("Looking up entity using id (" + entity.get("_id") + "): " + entities.get(entity.get("_id")));
            console.log("Looking up entity using cid (" + entity.cid + "): " + entities.get(entity.cid));
            console.log("");
        });
    }});
}});

When I run this, I get: 运行此命令时,我得到:

Test result 测试结果

entity.id = undefined
entity.get("_id") = 528146ade34176b255000003
Looking up entity using id (528146ade34176b255000003): undefined 
Looking up entity using cid (c1): undefined

entity.id = undefined
entity.get("_id") = 528146ade34176b255000002
Looking up entity using id (528146ade34176b255000002): undefined
Looking up entity using cid (c2): undefined

entity.id = undefined
entity.get("_id") = 528146ade34176b255000001
Looking up entity using id (528146ade34176b255000001): undefined
Looking up entity using cid (c3): undefined

entity.id = undefined
entity.get("_id") = 528146ade34176b255000004
Looking up entity using id (528146ade34176b255000004): undefined
Looking up entity using cid (c4): undefined 

What I would expect is for the collection to return the specific entity. 我希望集合可以返回特定的实体。 Does it have something to do with my special idAttribute "_id" (for compliance with mongodb)? 它与我的特殊idAttribute“ _id”(符合mongodb)有关吗?

EDIT 编辑

Apparently it seems to have something to do with my idAttribute. 显然,这似乎与我的idAttribute有关。 Because if I add an "id" field to every entity returned, I can lookup entities using its id. 因为如果我向返回的每个实体添加“ id”字段,则可以使用其id查找实体。 Like so on server side: 在服务器端类似:

function getEntitiesInSite(siteId, fun) {
    db.siteentities.find({site: mongojs.ObjectId(siteId)}, function(err, entities) {
        entities.forEach(function (entity) {
            entity.id = entity._id;
        });
        fun(err, entities);
    });
}

This isn't exactly what I wanted and I can imagine that I would run into other problems in the future with inconsistent id's (having both id and _id fields). 这不完全是我想要的,我可以想象,将来如果ID不一致(同时具有id_id字段),我还会遇到其他问题。

the root of problems is your models' id attribute is not set. 问题的根源是未设置模型的id属性。

id is only checked and set to your idAttribute when the model's set method is called. 仅在idAttribute模型的set方法时,才检查id并将其设置为idAttribute

When you fetch from collection successfully, there is no call to set function of the model. 成功从集合中获取数据时,不会调用模型的set函数。 So id is undefined for models. 因此,对于模型, idundefined的。

you need to trigger set event in each of your entity model in order for this to work. 您需要在每个entity模型中触发set事件,以使其起作用。

I found out I was running an old version of backbone.js. 我发现我正在运行旧版本的ribs.js。 Which obviously couldn't be noticed by anyone else, since I didn't add that information to the question. 显然其他人都不会注意到,因为我没有将这些信息添加到问题中。

With backbone.js 1.1.0 and underscore 1.4.4 everything works just fine. 有了骨干.js 1.1.0和下划线1.4.4,一切都很好。

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

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