Created
June 28, 2010 16:22
-
-
Save iamjwc/456047 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: Todos.TaskDataSource | |
// Copyright: ©2010 My Company, Inc. | |
// ========================================================================== | |
/*globals Todos */ | |
/** @class | |
(Document Your Data Source Here) | |
@extends SC.DataSource | |
*/ | |
sc_require('models/task'); | |
Todos.TASKS_QUERY = SC.Query.remote(Todos.Task, { | |
orderBy: 'isDone,description' | |
}); | |
Todos.TaskDataSource = SC.DataSource.extend( | |
/** @scope Todos.TaskDataSource.prototype */ { | |
// .......................................................... | |
// QUERY SUPPORT | |
// | |
fetch: function(store, query) { | |
if (query === Todos.TASKS_QUERY) { | |
SC.Request.getUrl('/tasks').header({'Accept': 'application/json'}).json() | |
.notify(this, 'didFetchTasks', store, query) | |
.send(); | |
return YES; | |
} | |
return NO; | |
}, | |
didFetchTasks: function(response, store, query) { | |
if (SC.ok(response)) { | |
var storeKeys = store.loadRecords(Todos.Task, response.get('body').content); | |
store.loadQueryResults(query, storeKeys); | |
} else store.dataSourceDidErrorQuery(query, response); | |
}, | |
// .......................................................... | |
// RECORD SUPPORT | |
// | |
retrieveRecord: function(store, storeKey) { | |
console.log("retrieve!") | |
if (SC.kindOf(store.recordTypeFor(storeKey), Todos.Task)) { | |
var url = store.idFor(storeKey); | |
console.log(url); | |
SC.Request.getUrl(url).header({ | |
'Accept': 'application/json' | |
}).json() | |
.notify(this, 'didRetrieveTask', store, storeKey) | |
.send(); | |
return YES; | |
} else return NO; | |
}, | |
didRetrieveTask: 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), Todos.Task)) { | |
SC.Request.postUrl('/tasks').header({ 'Accept': 'application/json' }).json() | |
.notify(this, this.didCreateTask, store, storeKey) | |
.send(store.readDataHash(storeKey)); | |
return YES; | |
} else return NO; | |
}, | |
didCreateTask: function(response, store, storeKey) { | |
if (SC.ok(response)) { | |
var id = response.get('body').content.id | |
store.dataSourceDidComplete(storeKey, response.get('body').content, id); // update id | |
//store.loadRecords(Todos.Task, [response.get('body').content]); | |
} else { | |
store.dataSourceDidError(storeKey, response); | |
} | |
}, | |
updateRecord: function(store, storeKey) { | |
console.log(storeKey); | |
if (SC.kindOf(store.recordTypeFor(storeKey), Todos.Task)) { | |
SC.Request.putUrl(store.idFor(storeKey)).header({ | |
'Accept': 'application/json' | |
}).json() | |
.notify(this, this.didUpdateTask, store, storeKey) | |
.send(store.readDataHash(storeKey)); | |
return YES; | |
} else return NO ; | |
}, | |
didUpdateTask: 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), Todos.Task)) { | |
SC.Request.deleteUrl(store.idFor(storeKey)).header({ | |
'Accept': 'application/json' | |
}).json() | |
.notify(this, this.didDestroyTask, store, storeKey) | |
.send(); | |
return YES; | |
} else return NO; | |
}, | |
didDestroyTask: function(response, store, storeKey) { | |
if (SC.ok(response)) { | |
store.dataSourceDidDestroy(storeKey); | |
} else store.dataSourceDidError(response); | |
} | |
}) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment