Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==========================================================================
- // Project: Spanish.WordDataSource
- // Copyright: ©2011 My Company, Inc.
- // ==========================================================================
- /*globals Spanish */
- /** @class
- (Document Your Data Source Here)
- @extends SC.DataSource
- */
- sc_require('models/word');
- Spanish.WORDS_QUERY = SC.Query.local(Spanish.Word,{
- orderBy: 'word'
- });
- Spanish.WordDataSource = SC.DataSource.extend(
- /** @scope Spanish.WordDataSource.prototype */ {
- // ..........................................................
- // QUERY SUPPORT
- //
- fetch: function(store, query) {
- if(query === Spanish.WORDS_QUERY){
- SC.Request.getUrl('/words').header({'Accept': 'application/json'}).json()
- .notify(this, 'didFetchWords', store, query)
- .send();
- return YES;
- }
- return NO ; // return YES if you handled the query
- },
- didFetchWords: function(response, store, query){
- if(SC.ok(response)){
- var storeKeys = store.loadRecords(Spanish.Word, response.get('body'));
- store.loadQueryResults(query, storeKeys);
- }else{
- store.dataSourceDidErrorQuery(query, response);
- }
- },
- // ..........................................................
- // RECORD SUPPORT
- //
- retrieveRecord: function(store, storeKey) {
- if(SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)){
- var url = '/words/' + store.idFor(storeKey);
- SC.Request.get(url).header({
- 'Accept':'application/json'
- }).json()
- .notify(this, 'didRetrieveWord', store, storeKey)
- .send();
- return YES;
- }else{
- return NO ; // return YES if you handled the storeKey
- }
- },
- didRetrieveWord: function(response, store, storeKey){
- if(SC.ok(response)){
- var dataHash = response.get('body').content;
- store.dataSourceDidComplete(storeKey, dataHash);
- }else{
- store.dataSourceDidError(storeKey, response);
- }
- },
- createRecord: function(store, storeKey) {
- if (SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)) {
- SC.Request.postUrl('/words').header({
- 'Accept': 'application/json'
- }).json()
- .notify(this, "didCreateWord", store, storeKey)
- .send(store.readDataHash(storeKey));
- return YES;
- } else{
- return NO;
- }
- },
- didCreateWord: 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);
- },
- // ..........................................................
- // UPDATE RECORDS
- //
- updateRecord: function(store, storeKey) {
- if (SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)) {
- SC.Request.putUrl(store.idFor(storeKey)).header({
- 'Accept': 'application/json'
- }).json()
- .notify(this, this.didUpdateWord, store, storeKey)
- .send(store.readDataHash(storeKey));
- return YES;
- } else return NO ;
- },
- didUpdateWord: function(response, store, storeKey) {
- if (SC.ok(response)) {
- var data = response.get('body');
- if (data) data = data.content; // if hash is returned; use it.
- store.dataSourceDidComplete(storeKey, data) ;
- } else store.dataSourceDidError(storeKey);
- },
- // ..........................................................
- // DESTROY RECORDS
- //
- destroyRecord: function(store, storeKey) {
- if (SC.kindOf(store.recordTypeFor(storeKey), Spanish.Word)) {
- SC.Request.deleteUrl(store.idFor(storeKey)).header({
- 'Accept': 'application/json'
- }).json()
- .notify(this, this.didDestroyWord, store, storeKey)
- .send();
- return YES;
- } else return NO;
- },
- didDestroyWord: function(response, store, storeKey) {
- if (SC.ok(response)) {
- store.dataSourceDidDestroy(storeKey);
- } else store.dataSourceDidError(response);
- }
- }) ;
Add Comment
Please, Sign In to add comment