Enyo不同的系列,具有相同的型号

时间:2014-04-21 12:05:01

标签: javascript enyo

我一直在尝试使用通用模型创建两个集合。我收到以下错误:

“未捕获的enyo.Store.addRecord:重复记录已添加到存储类型app.ImageModel,其中primaryKey设置为id,并且67774271的相同值不能与没有存在的类型共存商店的ignoreDuplicates标记设置为true“。

以下是我定义的两个集合......

enyo.kind({
    name: "app.FeatureCollection",
    kind: "enyo.Collection",
    model: "app.ImageModel",
    defaultSource: "appF",
    ...
    ...
});


enyo.kind({
    name: "app.SearchCollection",
    kind: "enyo.Collection",
    model: "app.ImageModel",
    defaultSource: "appS",
    ...
    ... 
});

我使用的模型如下:

 enyo.kind({
    name: "app.ImageModel",
    kind: "enyo.Model",
    readOnly: true,
    ....
    ....
 });

有一次我设置如下:

this.set("data", new app.FeatureCollection());

在另一个,

this.set("data", new app.SearchCollection());

我无法找出可能产生错误的内容。我甚至试图在模型中将“ignoreDuplicates”设置为true ...但仍然会出现错误。任何可能出错的建议。

2 个答案:

答案 0 :(得分:2)

ignoreDuplicates标记应在enyo.Store上设置,而不是enyo.Model


enyo.store.ignoreDuplicates = true;

您是否使用fetch enyo.Collection方法检索数据?如果是这样,您可以考虑在提取调用中将strategy属性设置为merge,以便为数据集中的每个唯一图像创建一条记录,即:


myCollection.fetch({strategy: "merge", success: function(rec, opts, res) {
    // do something after data is retrieved
}});

答案 1 :(得分:0)

我没有看到您提供的代码有问题。我在jsFiddle上创建了一个示例,它按预期工作。

http://jsfiddle.net/z7WwZ/

也许问题出现在代码的其他部分?

enyo.kind({
    name: "app.FeatureCollection",
    kind: "enyo.Collection",
    model: "app.MyModel"
});


enyo.kind({ 
    name: "app.SearchCollection",
    kind: "enyo.Collection",
    model: "app.MyModel"
});

enyo.kind({
    name: "app.MyModel",
    kind: "enyo.Model",
    readOnly: true,
    defaults: {
        firstName: "Unknown",
        lastName: "Unknown"
    }
});

enyo.kind({
    name: "App",
    components: [],
        bindings: [],
    create: enyo.inherit(function (sup) {
        return function () {
            sup.apply(this, arguments);
            this.collection1 = new app.FeatureCollection(this.data1);
            enyo.log("Collection1(0) >>> " + this.collection1.at(0).get("lastName"));
            this.collection1.at(0).set("lastName", "Smith");
            enyo.log("Collection1(0) >>> " + this.collection1.at(0).get("lastName"));

        this.collection2 = new app.SearchCollection(this.data2);
        enyo.log("Collection2(0) >>> " + this.collection2.at(0).get("lastName"));
        this.collection1.at(0).set("lastName", "Jones");
        enyo.log("Collection2(0) >>> " + this.collection1.at(0).get("lastName"));
        };
    }),
    data1: [{
        firstName: "Hall",
        lastName: "Caldwell"
    }, {
        firstName: "Felicia",
        lastName: "Fitzpatrick"
    }, {
        firstName: "Delgado",
        lastName: "Cole"
    }],

    data2: [{
        firstName: "Alejandra",
        lastName: "Walsh"
    }, {
        firstName: "Marquez",
        lastName: "James"
    }, {
        firstName: "Barr",
        lastName: "Lott"
    }]

});

new App().renderInto(document.body);