简体   繁体   中英

Querying a store in dojo to find a number that is less than a given number

Given an example in 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 using DataStore http://livedocs.dojotoolkit.org/dojo/store/DataStore

DataStore contains a store of type ItemFileWriteStore http://livedocs.dojotoolkit.org/dojo/data/ItemFileWriteStore

I'm trying the following:

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

and that doesn't really work.

As far as I can see SimpleQueryEngine uses dojo/_base/array which uses a filter method that takes in parameters such as an array and callback function to filter by.

As you can see here: https://github.com/dojo/dojo/blob/master/store/DataStore.js , DataStore uses SimpleQueryEngine so it should work...

My store contains objects like this:

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

I would like to query given store to find a number that is less than a given number.

And to clarify what I'm really trying to understand is why passing in a function as a parameter to query() method doesn't work and how to make it work.

Thanks,

Since you have an ItemFileWriteStore , you could use it's function called fetch . It might be able to do what you need. Let's say store is your ItemFileWriteStore , you would do:

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

In your onComplete function you would have the array of sorted store items in descending order based on the number parameter. At this point it would be trivial to find all numbers that are less than your specified number (rather than just 1 as you stated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM