【问题标题】:Fetching collection with backbone.js but id for each model is not set使用backbone.js 获取集合,但未设置每个模型的ID
【发布时间】:2013-11-12 09:40:01
【问题描述】:

简而言之,我正在获取一个集合,但之后我无法使用其 idcid 从集合中获取特定实体。

对于这个问题,考虑我的两个 REST 端点 /sites/sites/some-site-id/entities sitesentities 都是集合。这是我的模型/系列:

模型/系列

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"
});

我的问题是,当我为网站获取实体时,我无法使用集合上的 get 方法从集合中查找某个实体。我只是返回了undefined

这是我测试它的方式(entities.fetch 将获得 4 个实体):

测试代码

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("");
        });
    }});
}});

当我运行它时,我得到:

测试结果

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 

我期望集合返回特定实体。是不是和我的特殊idAttribute“_id”有关(为了符合mongodb)?

编辑

显然这似乎与我的 idAttribute 有关。因为如果我为每个返回的实体添加一个“id”字段,我可以使用它的 id 查找实体。像服务器端这样:

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);
    });
}

这并不是我想要的,我可以想象我将来会遇到其他问题,因为 id 不一致(同时具有 id_id 字段)。

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    问题的根源是您的模型的id 属性未设置。

    id 仅在调用模型的 set 方法时被检查并设置为您的 idAttribute

    当你成功从集合中获取时,没有调用模型的set函数。所以 id 对于模型来说是 undefined

    您需要在您的每个 entity 模型中触发 set 事件才能使其工作。

    【讨论】:

      【解决方案2】:

      我发现我运行的是旧版本的主干.js。这显然不会被其他人注意到,因为我没有在问题中添加该信息。

      backbone.js 1.1.0 和 underscore 1.4.4 一切正常。

      【讨论】:

        猜你喜欢
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        • 2011-11-30
        • 2012-01-04
        • 1970-01-01
        相关资源
        最近更新 更多