define("dojox/data/QueryReadStore", ["dojo", "dojox", "dojo.data.util.sorter", "dojo/string"], function(dojo, dojox) {
dojo.declare("dojox.data.QueryReadStore",
null,
{
// summary:
// This class provides a store that is mainly intended to be used
// for loading data dynamically from the server, used i.e. for
// retreiving chunks of data from huge data stores on the server (by server-side filtering!).
// Upon calling the fetch() method of this store the data are requested from
// the server if they are not yet loaded for paging (or cached).
//
// For example used for a combobox which works on lots of data. It
// can be used to retreive the data partially upon entering the
// letters "ac" it returns only items like "action", "acting", etc.
//
// note:
// The field name "id" in a query is reserved for looking up data
// by id. This is necessary as before the first fetch, the store
// has no way of knowing which field the server will declare as
// identifier.
//
// example:
// | // The parameter "query" contains the data that are sent to the server.
// | var store = new dojox.data.QueryReadStore({url:'/search.php'});
// | store.fetch({query:{name:'a'}, queryOptions:{ignoreCase:false}});
//
// | // Since "serverQuery" is given, it overrules and those data are
// | // sent to the server.
// | var store = new dojox.data.QueryReadStore({url:'/search.php'});
// | store.fetch({serverQuery:{name:'a'}, queryOptions:{ignoreCase:false}});
//
// |
// | jsId="store2"
// | url="../tests/stores/QueryReadStore.php"
// | requestMethod="post">
// |
// | jsId="model2"
// | store="store2"
// | sortFields="[{attribute: 'name', descending: true}]"
// | rowsPerPage="30">
// |
// | model="model2"
// | structure="gridLayout"
// | style="height:300px; width:800px;">
//
// todo:
// - there is a bug in the paging, when i set start:2, count:5 after an initial fetch() and doClientPaging:true
// it returns 6 elemetns, though count=5, try it in QueryReadStore.html
// - add optional caching
// - when the first query searched for "a" and the next for a subset of
// the first, i.e. "ab" then we actually dont need a server request, if
// we have client paging, we just need to filter the items we already have
// that might also be tooo much logic
url:"",
requestMethod:"get",
//useCache:false,
// We use the name in the errors, once the name is fixed hardcode it, may be.
_className:"dojox.data.QueryReadStore",
// This will contain the items we have loaded from the server.
// The contents of this array is optimized to satisfy all read-api requirements
// and for using lesser storage, so the keys and their content need some explaination:
// this._items[0].i - the item itself
// this._items[0].r - a reference to the store, so we can identify the item
// securly. We set this reference right after receiving the item from the
// server.
_items:[],
// Store the last query that triggered xhr request to the server.
// So we can compare if the request changed and if we shall reload
// (this also depends on other factors, such as is caching used, etc).
_lastServerQuery:null,
// Store how many rows we have so that we can pass it to a clientPaging handler
_numRows:-1,
// Store a hash of the last server request. Actually I introduced this
// for testing, so I can check if no unnecessary requests were issued for
// client-side-paging.
lastRequestHash:null,
// summary:
// By default every request for paging is sent to the server.
doClientPaging:false,
// summary:
// By default all the sorting is done serverside before the data is returned
// which is the proper place to be doing it for really large datasets.
doClientSorting:false,
// Items by identify for Identify API
_itemsByIdentity:null,
// Identifier used
_identifier:null,
_features: {'dojo.data.api.Read':true, 'dojo.data.api.Identity':true},
_labelAttr: "label",
constructor: function(/* Object */ params){
dojo.mixin(this,params);
},
getValue: function(/* item */ item, /* attribute-name-string */ attribute, /* value? */ defaultValue){
// According to the Read API comments in getValue() and exception is
// thrown when an item is not an item or the attribute not a string!
this._assertIsItem(item);
if(!dojo.isString(attribute)){
throw new Error(this._className+".getValue(): Invalid attribute, string expected!");
}
if(!this.hasAttribute(item, attribute)){
// read api says: return defaultValue "only if *item* does not have a value for *attribute*."
// Is this the case here? The attribute doesn't exist, but a defaultValue, sounds reasonable.
if(defaultValue){
return defaultValue;
}
}
return item.i[attribute];
},
getValues: function(/* item */ item, /* attribute-name-string */ attribute){
this._assertIsItem(item);
var ret = [];
if(this.hasAttribute(item, attribute)){
ret.push(item.i[attribute]);
}
return ret;
},
getAttributes: function(/* item */ item){
this._assertIsItem(item);
var ret = [];
for(var i in item.i){
ret.push(i);
}
return ret;
},
hasAttribute: function(/* item */ item, /* attribute-name-string */ attribute){
// summary:
// See dojo.data.api.Read.hasAttribute()
return this.isItem(item) && typeof item.i[attribute]!="undefined";
},
containsValue: function(/* item */ item, /* attribute-name-string */ attribute, /* anything */ value){
var values = this.getValues(item, attribute);
var len = values.length;
for(var i=0; i
if(values[i] == value){
return true;
}
}
return false;
},
isItem: function(/* anything */ something){
// Some basic tests, that are quick and easy to do here.
// >>> var store = new dojox.data.QueryReadStore({});
// >>> store.isItem("");
// false
//
// >>> var store = new dojox.data.QueryReadStore({});
// >>> store.isItem({});
// false
//
// >>> var store = new dojox.data.QueryReadStore({});
// >>> store.isItem(0);
// false
//
// >>> var store = new dojox.data.QueryReadStore({});
// >>> store.isItem({name:"me", label:"me too"});
// false
//
if(something){
return typeof something.r != "undefined" && something.r == this;
}
return false;