Created
February 4, 2011 01:42
-
-
Save framallo/810598 to your computer and use it in GitHub Desktop.
This file contains 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: Todozen.StoryDataSource | |
// Copyright: ©2011 My Company, Inc. | |
// ========================================================================== | |
/*globals Todozen */ | |
/** @class | |
(Document Your Data Source Here) | |
@extends SC.DataSource | |
*/ | |
sc_require('models/story'); | |
Todozen.STORIES_QUERY = SC.Query.local(Todozen.Story); | |
Todozen.StoryDataSource = SC.DataSource.extend( | |
/** @scope Todozen.StoryDataSource.prototype */ { | |
// .......................................................... | |
// QUERY SUPPORT | |
// | |
fetch: function(store, query) { | |
if (query === Todozen.STORIES_QUERY) { | |
SC.Request.getUrl('/stories').header({'Accept': 'application/json'}).json() | |
.notify(this, 'didFetchStories', store, query) | |
.send(); | |
return YES; | |
} | |
return NO ; // return YES if you handled the query | |
}, | |
didFetchStory: function(response, store, query) { | |
console.log(response.get('body')); | |
if (SC.ok(response)) { | |
store.loadRecords(Todozen.Story, response.get('body').content); | |
store.dataSourceDidFetchQuery(query); | |
} else store.dataSourceDidErrorQuery(query, response); | |
}, | |
// .......................................................... | |
// RECORD SUPPORT | |
// | |
retrieveRecord: function(store, storeKey) { | |
// TODO: Add handlers to retrieve an individual record's contents | |
// call store.dataSourceDidComplete(storeKey) when done. | |
return NO ; // return YES if you handled the storeKey | |
}, | |
createRecord: function(store, storeKey) { | |
if (SC.kindOf(store.recordTypeFor(storeKey), Todozen.Story)) { | |
SC.Request.postUrl('/stories').header({ | |
'Accept': 'application/json' | |
}).json() | |
.notify(this, this.didCreateStory, store, storeKey) | |
.send({story:store.readDataHash(storeKey)}); | |
return YES; | |
} else return NO; | |
}, | |
didCreateStory: function(response, store, storeKey) { | |
if (SC.ok(response)) { | |
// Adapted from parseUri 1.2.2 | |
// (c) Steven Levithan <stevenlevithan.com> | |
// MIT License | |
var parser = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/; | |
var url = parser.exec(response.header('location'))[8]; | |
store.dataSourceDidComplete(storeKey, null, url); // update url | |
} else store.dataSourceDidError(storeKey, response); | |
}, | |
updateRecord: function(store, storeKey) { | |
// TODO: Add handlers to submit modified record to the data source | |
// call store.dataSourceDidComplete(storeKey) when done. | |
return NO ; // return YES if you handled the storeKey | |
}, | |
destroyRecord: function(store, storeKey) { | |
// TODO: Add handlers to destroy records on the data source. | |
// call store.dataSourceDidDestroy(storeKey) when done | |
return NO ; // return YES if you handled the storeKey | |
} | |
}) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment