Skip to content

Instantly share code, notes, and snippets.

@rvega
Created November 22, 2011 16:55
Show Gist options
  • Save rvega/1386180 to your computer and use it in GitHub Desktop.
Save rvega/1386180 to your computer and use it in GitHub Desktop.
/**
* Get list of resources
*/
fetch: function(store, query) {
store.loadQueryResults(query, SC.SparseArray.create({delegate: this, store: store, query: query, rangeWindowSize:20}));
return YES;
},
sparseArrayDidRequestLength: function(sparseArray) {
this.sparseArrayDidRequestRange(sparseArray, { start: 0, length: 20 });
},
sparseArrayDidRequestRange: function(sparseArray, range) {
var url = this._figureOutURL(sparseArray.query.recordType);
var page = Math.ceil(range.start/range.length) + 1;
var querystring = '?pagina='+ page +'&cuantos=' + range.length;
url = url+querystring;
SC.Request.getUrl(url)
.set('isJSON', YES)
.notify(this, this._didFetch, { sparseArray:sparseArray })
.send();
},
_didFetch: function(response, params){
var sparseArray = params.sparseArray;
if(response.status == 200){
var count = response.get('body').cnt;
var content = response.get('body').c;
// id should not be a part of the data hash in sproutcore.
var records = new Array();
var ids = new Array();
for(var i=0; i<content.length; i++) {
var record = content[i];
var id = record.id;
delete(record.id);
records.push(record);
ids.push(id);
};
// Starting index for the sparseArray
var address = response.address();
var regex = new RegExp("[\\?&]pagina=([^&#]*)");
var page = regex.exec(address);
page = page[1];
regex = new RegExp("[\\?&]cuantos=([^&#]*)");
var howMany = regex.exec(address);
howMany = howMany[1];
var start = howMany * (page - 1);
var range = {start:start, length:howMany};
sparseArray.provideLength(count);
var storeKeys = sparseArray.get('store').loadRecords(Core.Usuario, records, ids);
sparseArray.provideObjectsInRange({ start: range.start, length: range.length }, storeKeys);
sparseArray.rangeRequestCompleted(range.start);
}
return YES;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment