Created
August 30, 2011 20:10
-
-
Save jsmestad/1181887 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.ajaxSetup({ | |
| cache: false, | |
| dataType: 'json' | |
| }); | |
| SC.RestDataSource = SC.DataSource.extend({ | |
| storagePrefix: null, | |
| pluralResourceName: function(recordType) { | |
| return recordType.pluralResourceName || SC.String.pluralize(recordType.resourceName); | |
| }, | |
| resourceURL: function(recordType, store, storeKey) { | |
| var id, resourceName = this.pluralResourceName(recordType), | |
| prefix = this.get('storagePrefix'); | |
| if (!resourceName) { | |
| throw new SC.Error("You have to define resourceName on %@ ...".fmt(recordType)); | |
| } | |
| if (storeKey) { | |
| id = store.idFor(storeKey); | |
| } | |
| if (prefix) { | |
| resourceName = '%@/%@'.fmt(prefix, resourceName); | |
| } | |
| if (id) { | |
| return '/%@/%@'.fmt(resourceName, id); | |
| } | |
| return '/%@'.fmt(resourceName); | |
| }, | |
| fetch: function(store, query) { | |
| var recordType = query.get('recordType'); | |
| var url = this.resourceURL(recordType, store); | |
| $.ajax({ url: url }).done(function(response) { | |
| store.loadRecords(recordType, response); | |
| store.dataSourceDidFetchQuery(query); | |
| }); | |
| return YES; | |
| }, | |
| retrieveRecord: function(store, storeKey, id) { | |
| var recordType = store.recordTypeFor(storeKey); | |
| var url = this.resourceURL(store.recordTypeFor(storeKey), store, storeKey); | |
| $.ajax({ url: url }).done(function(response) { | |
| store.loadRecords(recordType, response); | |
| store.dataSourceDidComplete(storeKey, response); | |
| }); | |
| return YES; | |
| }, | |
| createRecord: function(store, storeKey, params) { | |
| return this._createOrUpdateRecord(store, storeKey); | |
| }, | |
| updateRecord: function(store, storeKey, params) { | |
| return this._createOrUpdateRecord(store, storeKey, true); | |
| }, | |
| destroyRecord: function(store, storeKey, params) { | |
| var url = this.resourceURL(store.recordTypeFor(storeKey), store, storeKey); | |
| $.ajax({ type: 'DELETE', url: url }) | |
| .done( function(response) { | |
| store.dataSourceDidDestroy(storeKey); | |
| }); | |
| return YES; | |
| }, | |
| _createOrUpdateRecord: function(store, storeKey, update) { | |
| var recordType = store.recordTypeFor(storeKey); | |
| var url = this.resourceURL(recordType, store, storeKey); | |
| var resource = {}; | |
| resource[recordType.resourceName] = store.readDataHash(storeKey); | |
| $.ajax({ type: (update ? 'PUT' : 'POST'), url: url, data: resource}) | |
| .done( function(response) { | |
| if (update) { | |
| if (SC.ok(response)) { | |
| store.dataSourceDidComplete(storeKey); | |
| } else { | |
| store.dataSourceDidError(storeKey); | |
| } | |
| } else { | |
| store.loadRecords(recordType, response); | |
| store.dataSourceDidComplete(storeKey); | |
| } | |
| }); | |
| return YES; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment