Skip to content

Instantly share code, notes, and snippets.

@geoffreyd
Forked from erichocean/gist:11956
Created September 22, 2008 07:16
Show Gist options
  • Save geoffreyd/11957 to your computer and use it in GitHub Desktop.
Save geoffreyd/11957 to your computer and use it in GitHub Desktop.
// ..........................................
// REFRESH
refreshRecords: function(records, options) {
if (!records || records.length == 0) return ;
if (!options) options = {} ;
records = records.byResourceURL() ; // group by resource.
for(var resource in records) {
if (resource == '*') continue ;
var curRecords = records[resource] ;
// collect resource ids, sort records into hash, and get cacheCode.
var cacheCode = null ; var ids = [] ;
var primaryKey = curRecords[0].get('primaryKey') ; // assumes all the same
curRecords.each(function(r) {
cacheCode = cacheCode || r._cacheCode ;
var key = r.get(primaryKey);
if (key) { ids.push(key); }
});
var context = {
recordType: curRecords[0].recordType, // default record type
onSuccess: options.onSuccess,
onFailure: options.onFailure
};
var params = {
requestContext: context,
cacheCode: ((cacheCode=='') ? null : cacheCode),
_onSuccess: this._refreshSuccess.bind(this),
_onFailure: this._refreshFailure.bind(this)
};
if (ids.length == 1 && curRecords[0].refreshURL) params['url'] = curRecords[0].refreshURL;
// issue request
this.request(resource, this._refreshAction, ids, params, this._refreshMethod) ;
}
},
_refreshAction: 'show',
_refreshMethod: 'get',
// This method is called when a refresh is successful. It expects an array
// of hashes, which it will convert to records.
_refreshSuccess: function(transport, cacheCode, context) {
var json = eval('json='+transport.responseText) ;
if (!json) { console.log('invalid json!'); return; }
if (json.records) this.refreshRecordsWithData(json.records, context.recordType, cacheCode, true);
if (context.onSuccess) context.onSuccess(transport, cacheCode) ;
},
_refreshFailure: function(transport, cacheCode, context) {
console.log('refreshFailed!') ;
if (context.onFailure) context.onFailure(transport, cacheCode) ;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment