Skip to content

Instantly share code, notes, and snippets.

@philbritton
Created December 5, 2013 00:10
Show Gist options
  • Select an option

  • Save philbritton/7797967 to your computer and use it in GitHub Desktop.

Select an option

Save philbritton/7797967 to your computer and use it in GitHub Desktop.
Ext.define('App.ODataProxy', { extend:'Ext.data.proxy.Ajax', requires:[ 'Ext.data.reader.Json', 'Ext.data.writer.Json' ], alias:'proxy.odata', sortParam:'$orderby', filterParam:'$filter', pageParam:null, limitParam:'$top', startParam:'$skip', actionMethods: { create:'POST', read:'GET', //Changed from PUT to allow writer to not writeAllFields. update: Ext.isIE8m ? 'PUT' : 'PATCH', destroy:'DELETE' }, headers:{ Accept:'application/json;odata=verbose', Prefer:'return-content' }, constructor:function(cfg) { cfg.reader = { type:'json', readRecords:function(data) { if (data.status == 204) { return null; } return this.self.prototype.readRecords.call(this, data['d']); }, buildExtractors:function() { this.self.prototype.buildExtractors.apply(this, arguments); this.getRoot = this.getRootFunc; }, getRootFunc:function(root) { return root['results'] || [root]; }, totalProperty:'__count', metaProperty:'__metadata' }; cfg.writer = { type:'json', writeAllFields:Ext.isIE8m }; cfg.extraParams = Ext.apply({ "$inlinecount":'allpages' },cfg.extraParams); return this.callParent(arguments); }, buildUrl:function(request) { var me = this, operation = request.operation, records = operation.records || [], record = records[0], url = me.url, id = record ? record.getId() : operation.id; if (id) { url = url.replace(/\/?$/,'(' + id + ')'); } if (request.url) { url += request.url; } request.url = url; return me.callParent(arguments); }, encodeSorters:function(sorters) { var sch, buf = [], sorter; for (sch = 0; sorter = sorters[sch]; sch++) { buf.push(sorter.property + ' ' + sorter.direction.toLowerCase()); } return buf.join(','); }, encodeFilters:function(filters) { var sch, filterChecks = [], filterBuf, filter, exact; for (sch = 0; filter = filters[sch]; sch++) { exact = filter.exactMatch; filterBuf = []; if (filter.exactMatch) { filterBuf.push('('); filterBuf.push(filter.value); filterBuf.push(' eq '); } else { filterBuf.push('substringof(\''); filterBuf.push(filter.caseSensitive ? filter.value : filter.value.toLowerCase()); filterBuf.push('\','); } if (!filter.caseSensitive && !exact) { filterBuf.push('tolower('); } filterBuf.push(filter.property); if (!filter.caseSensitive && !exact) { filterBuf.push(')'); } filterBuf.push(')'); filterChecks.push(filterBuf.join('')); } return filterChecks.join(' and '); } });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment