簡體   English   中英

使用骨架.js獲取集合,但未設置每個模型的ID

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

簡而言之,我正在獲取一個集合,但是此后,我無法使用其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字段),我還會遇到其他問題。

問題的根源是未設置模型的id屬性。

僅在idAttribute模型的set方法時,才檢查id並將其設置為idAttribute

成功從集合中獲取數據時,不會調用模型的set函數。 因此,對於模型, idundefined的。

您需要在每個entity模型中觸發set事件,以使其起作用。

我發現我正在運行舊版本的ribs.js。 顯然其他人都不會注意到,因為我沒有將這些信息添加到問題中。

有了骨干.js 1.1.0和下划線1.4.4,一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM