Created
July 13, 2009 09:46
-
-
Save sudara/146028 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
| // ========================================================================== | |
| // Project: SproutCore - JavaScript Application Framework | |
| // Copyright: ©2006-2009 Sprout Systems, Inc. and contributors. | |
| // Portions ©2008-2009 Apple, Inc. All rights reserved. | |
| // License: Licened under MIT license (see license.js) | |
| // ========================================================================== | |
| /** @class | |
| TODO: Describe Class | |
| @extends SC.DataSource | |
| @since SproutCore 1.0 | |
| */ | |
| SC.Alonetone = SC.DataSource.extend( { | |
| // .......................................................... | |
| // STANDARD DATA SOURCE METHODS | |
| // | |
| requestCounter:0, | |
| cancelStoreKeys:{}, | |
| fetchRequest: SC.Request.getUrl("").set('isJSON', YES), | |
| /** | |
| Invoked by the store whenever it needs to retrieve an array of storeKeys | |
| matching a specific query. For the fixtures params are ignored and all | |
| storeKeys for the specific recordType are returned. | |
| @param {SC.Store} store the requesting store | |
| @param {Object} recordType key describing the request, may be SC.Record | |
| @param {Hash} params optional additonal fetch params | |
| @returns {SC.Array} result set with storeKeys. May be sparse. | |
| */ | |
| // fetchKey is a terrible name, really, since it's not a key | |
| // what it actually is: Either a SC.Query Object, or a model class name | |
| // like Listenapp.User | |
| fetch: function(store, fetchKey, params) { | |
| // So, in the case that this should be a local query and not hit | |
| // the server, but instead use records already in store | |
| if(SC.instanceOf(fetchKey, SC.Query) && (fetchKey.local == true)){ | |
| return store.storeKeysFor(fetchKey.recordType); | |
| } | |
| var ret = [], url; | |
| if (fetchKey === SC.Record.STORE_KEYS) { | |
| params.forEach(function(storeKey) { | |
| var recordType = SC.Store.recordTypeFor(storeKey), | |
| id = recordType.idFor(storeKey); | |
| //url=recordType.+id; | |
| this.fetchRequest.set('address', url); | |
| this.fetchRequest.notify(this, this.fetchRecordDidComplete, { | |
| store: store, fetchKey: fetchKey , storeKey: storeKey, id:id | |
| }).send(); | |
| this.cancelStoreKeys[this.generateRequestId(storeKey)]=this.fetchRequest; | |
| }, this); | |
| ret = params ; | |
| } else { | |
| recordType = fetchKey.recordType || fetchKey; | |
| // hack alert! Just faking it via <model name>s.json | |
| bullshitRestfulUrl = String(recordType).replace(/.*\./,'').toLowerCase()+'s.json'; | |
| url = fetchKey.url || bullshitRestfulUrl; | |
| // set the address | |
| this.fetchRequest.set('address', url); | |
| // make the request, calling back to this.fetchAllRecordsDidComplete | |
| // you can also pass a callback via the fetchKey | |
| this.fetchRequest.notify(this, this.fetchAllRecordsDidComplete, { | |
| store: store, fetchKey: fetchKey , storeKeyArray: ret | |
| }).send(); | |
| } | |
| return ret; | |
| }, | |
| /** | |
| Removes the request from the queue if it is cancelled. | |
| @param {SC.Store} store the store | |
| @param {Number} storeKey the store key | |
| @returns {Boolean} YES if supported | |
| */ | |
| cancel: function(store, storeKeys) { | |
| // TODO: The request manager should have methods to cancel request that are | |
| // in the queue or are being procesed, instead of accesing directly the queue | |
| // In case that a connection hangs there is no option to abort it. | |
| var i, j; | |
| for (i in storeKeys) { | |
| for (j in this.cancelStoreKeys) { | |
| if (i.indexOf(j) != -1) { | |
| SC.Request.manager.get('queue').removeObject(this.cancelStoreKeys[j]); | |
| this.cancelStoreKeys[j]=null; | |
| } | |
| } | |
| } | |
| return YES; | |
| }, | |
| createRequest: SC.Request.postUrl("tasks").set('isJSON', YES), | |
| /** | |
| Issues a request to create a record using the hash corresponding to the | |
| storeKey | |
| @param {SC.Store} store the store | |
| @param {Number} storeKey the store key | |
| @returns {Boolean} YES if supported | |
| */ | |
| createRecord: function(store, storeKey) { | |
| var dataHash = store.readDataHash(storeKey), | |
| obj={"content":dataHash}; | |
| this.createRequest.notify(this, this.createRecordDidComplete, { | |
| store: store, storeKey: storeKey | |
| }).send(SC.json.encode(obj)); | |
| this.cancelStoreKeys[this.generateRequestId(storeKey)]=this.createRequest; | |
| return YES ; | |
| }, | |
| updateRequest: SC.Request.putUrl("tasks").set('isJSON', YES), | |
| /** | |
| Issues a request to update the record corresponding to the storeKey. | |
| @param {SC.Store} store the store | |
| @param {Number} storeKey the store key | |
| @returns {Boolean} YES if supported | |
| */ | |
| updateRecord: function(store, storeKey) { | |
| var id = store.idFor(storeKey), | |
| dataHash = store.readDataHash(storeKey); | |
| this.updateRequest.notify(this, this.updateRecordDidComplete, { | |
| store: store, storeKey: storeKey, id:id | |
| }).send(SC.json.encode(dataHash)); | |
| this.cancelStoreKeys[this.generateRequestId(storeKey)]=this.updateRequest; | |
| return YES ; | |
| }, | |
| destroyRequest: SC.Request.deleteUrl("").set('isJSON', YES), | |
| /** | |
| Issues a request to delete the record corresponding to the storeKey | |
| @param {SC.Store} store the store | |
| @param {Number} storeKey the store key | |
| @returns {Boolean} YES if supported | |
| */ | |
| destroyRecord: function(store, storeKey) { | |
| var id = store.idFor(storeKey); | |
| if(!id) return YES; | |
| this.destroyRequest.set('address',id) ; | |
| this.destroyRequest.notify(this, this.destroyRecordDidComplete, { | |
| store: store, storeKey: storeKey | |
| }).send(); | |
| this.cancelStoreKeys[this.generateRequestId(storeKey)]=this.destroyRequest; | |
| return YES ; | |
| }, | |
| // callback methods | |
| /** | |
| Once the fetch request commint from store.retrieveRecords() | |
| is completed it handles the response and updates the store | |
| @param {SC.Request} fetch request | |
| @param {Object} hash with parameters {params.storeKey, params.store} | |
| @returns {Boolean} YES | |
| */ | |
| fetchRecordDidComplete: function(r,params) { | |
| console.log("BOOOO!"); | |
| debugger; | |
| var response, results, dataHash, storeKeys = [], hashes = []; | |
| response = r.response(); | |
| results = response.content; | |
| dataHash = results; | |
| hashes.push(dataHash); | |
| storeKeys.push(params.storeKey); | |
| params.store.dataSourceDidComplete(params.storeKey, dataHash, params.id); | |
| params.storeKeyArray.replace(0,0,storeKeys); | |
| return YES; | |
| }, | |
| /** | |
| Once the fetch request comming from store.findAll() | |
| is completed it handles the response and updates the store | |
| @param {SC.Request} fetch request | |
| @param {Object} hash with parameters {params.store} | |
| @returns {Boolean} YES | |
| */ | |
| fetchAllRecordsDidComplete: function(r,params) { | |
| console.log("RECORDS ARE HERE! YAY"); | |
| var hashes= [], storeKeys= [], dataHash, store, ret, primaryKey, | |
| response, results, lenresults, idx; | |
| // the response is actually the array of records | |
| response = r.response(); | |
| debugger; | |
| results = response.records; | |
| lenresults=results.length; | |
| // go through each record | |
| for(idx=0;idx<lenresults;idx++) { | |
| dataHash = results[idx]; | |
| hashes.push(dataHash); | |
| } | |
| // loadRecords: function(recordTypes, dataHashes, ids) { | |
| storeKeys = params.store.loadRecords(params.fetchKey.recordType, hashes); | |
| params.storeKeyArray.replace(0,0,storeKeys); | |
| // allow us to specify a callback in the SC.Query object | |
| if(params.fetchKey.callback != undefined){ | |
| params.fetchKey.callback.call(); | |
| } | |
| //TODO: add error handling | |
| return YES; | |
| }, | |
| /** | |
| Once the create request is completed it handles the response and updates the store | |
| @param {SC.Request} fetch request | |
| @param {Object} hash with parameters {params.storeKey, params.store} | |
| @returns {Boolean} YES | |
| */ | |
| createRecordDidComplete: function(r, params){ | |
| var dataHash, response, results, guid; | |
| dataHash = params.store.readDataHash(params.storeKey); | |
| response = r.response(); | |
| results = response.content; | |
| guid=results.guid; | |
| params.store.dataSourceDidComplete(params.storeKey, results, guid); | |
| return YES; | |
| }, | |
| /** | |
| Once the update request is completed it handles the response and updates the store | |
| @param {SC.Request} destroy request | |
| @param {Object} hash with parameters {params.storeKey, params.store, params.id} | |
| @returns {Boolean} YES | |
| */ | |
| updateRecordDidComplete: function(r, params){ | |
| var dataHash, response, results; | |
| dataHash = params.store.readDataHash(params.storeKey); | |
| response = r.response(); | |
| results = response.content; | |
| params.store.dataSourceDidComplete(params.storeKey, results, params.id); | |
| return YES; | |
| }, | |
| /** | |
| Once the destroy request is completed it handles the response and updates the store | |
| @param {SC.Request} destroy request | |
| @param {Object} hash with parameters {params.storeKey, params.store} | |
| @returns {Boolean} YES | |
| */ | |
| destroyRecordDidComplete: function(r, params){ | |
| params.store.dataSourceDidDestroy(params.storeKey); | |
| return YES; | |
| }, | |
| /** | |
| Generates an id for the passed record type. You can override this if | |
| needed. The default generates a storekey and formats it as a string. | |
| */ | |
| generateIdFor: function(recordType, dataHash, store, storeKey) { | |
| return "@id%@".fmt(SC.Store.generateStoreKey()); | |
| }, | |
| generateRequestId: function(storeKey){ | |
| this.requestCounter++; | |
| return storeKey+"_"+this.requestCounter; | |
| } | |
| }); | |
| var alonetoneServer=SC.Alonetone.create(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment