1

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

1 回答 1

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];

IE:

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从存储中检索到的对象的 a。

于 2012-06-12T09:26:02.310 回答