dojo存储getIdentity

时间:2012-06-12 09:13:59

标签: dojo

store.getIdentity()到底是什么: 假设我有以下商店,我可以用getIndetity做什么?

var store = new dojo.store.Memory({
    data: [
        {id: 1, name: "store1_item1", prime: false},
        {id: 2, name: "store1_item2", prime: true},
        {id: 3, name: "store1_item3", prime: true},
        {id: 4, name: "store1_item4", prime: false},
        {id: "xy", name: "store1_item5", prime: true}
    ]
});

1 个答案:

答案 0 :(得分:6)

来自dojo/store/Memory源代码:

// idProperty: String
//     Indicates the property to use as the identity property. The values of this
//     property should be unique.
idProperty: "id",

getIdentity: function(object){
// summary:
//     Returns an object's identity
// object: Object
//     The object to get the identity from
// returns: Number
return object[this.idProperty];

即:

var id = 1;
store.getIdentity(store.get(id)) == id;

您当然可以定义自己的idProperty

var store = new dojo.store.Memory({
    data: [
        {id: 1, name: "store1_item1", prime: false},
        {id: 2, name: "store1_item2", prime: true},
        {id: 3, name: "store1_item3", prime: true},
        {id: 4, name: "store1_item4", prime: false},
        {id: "xy", name: "store1_item5", prime: true}
    ],
    idProperty: "name"
});

然后:

var item = store.get("store1_item1"); // item == {id: 1, name: "store1_item1", prime: false}
store.getIdentity(item) == "store1_item1";

基本上,它应该返回从商店检索的对象的primary key