2014-01-24 53 views
3

給出的例子中http://dojotoolkit.org/reference-guide/1.9/dojo/store/util/SimpleQueryEngine查詢在道場商店找到一個數小於給定數目

SimpleQueryEngine(function(object){ 
    return object.id > 1; 
})(someData) // Returns an array with matching objects 

im使用數據存儲http://livedocs.dojotoolkit.org/dojo/store/DataStore

數據存儲包含類型的商店ItemFileWriteStorehttp://livedocs.dojotoolkit.org/dojo/data/ItemFileWriteStore

我在嘗試以下操作:

var myNumber = 12; // but could be any number like -12, 0.12, 12345 or -12345.1 
dataStore.store.query(function(storeItem){ 
    return storeItem.number < myNumber; 
}) 

這並不真正的工作。

據我所見,SimpleQueryEngine使用了dojo/_base/array,它使用了一個過濾方法,它接受諸如數組和過濾的回調函數之類的參數。

正如你可以在這裏看到:https://github.com/dojo/dojo/blob/master/store/DataStore.js,數據存儲區使用SimpleQueryEngine所以它應該工作...

我的商店包含這樣的對象:

[{id: 1, number: 2345},{id: 2, number: 23.45},{id: 3, number: -2345},{id: 4, number: 2345},{id: 5, number: 0.2345}] 

我想查詢給店裏找一個小於給定數字的數字。

並澄清我真正想明白的是爲什麼傳遞函數作爲query()方法的參數不起作用,以及如何使其工作。

感謝,

回答

2

既然你有一個ItemFileWriteStore,你可以用它叫fetch功能。它可能能夠做你需要的。比方說,store是你ItemFileWriteStore,你會怎麼做:

store.fetch({ 
    sort: [{attribute: "number", descending: true}] 
    onComplete: function(items, request) { 
     // items will be an array of the items in your store, 
     // sorted by the "number" attributed in descending order 
    } 
}); 

在你onComplete功能,您將有有序存放物品的降序,基於數參數順序排列。在這一點上,找到所有數字都小於您指定的數字(而不是像您所述的那樣僅爲1)將是微不足道的。

+0

你也可以用dataStore.store.query()獲得所有項目,但那不是。只是爲了澄清dataStore.store - DataStore,dataStore.store.store - ItemFileWriteStore。 – Eugene

+0

也許我不明白你想要做什麼?使用排序參數,您可以將所有項目按「編號」排序。換句話說,您可以輕鬆地遍歷並比較「item [i]」和「myNumber」,如果它小於myNumber,則將其存儲。一旦你在「myNumber」之上,你就停止循環。這不是最終目標還是我誤解了? –

+0

我想了解如何將一個函數作爲參數傳遞給dataStore.store.query()方法,該方法將執行所有的過濾操作,同時我試圖理解它爲什麼不起作用,對於混淆我感到抱歉,我會更新我的問題 – Eugene

相關問題