Skip to content

Instantly share code, notes, and snippets.

@tdegrunt
Created May 2, 2010 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdegrunt/387412 to your computer and use it in GitHub Desktop.
Save tdegrunt/387412 to your computer and use it in GitHub Desktop.
// ==========================================================================
// Project: Todos.TaskDataSource
// Copyright: ©2010 Tom de Grunt.
// ==========================================================================
/*globals Todos */
sc_require('models/task');
Todos.TASKS_QUERY = SC.Query.local(Todos.Task, {
orderBy: 'isDone,description'
});
/** @class
(Document Your Data Source Here)
@extends SC.DataSource
*/
Todos.TaskDataSource = SC.DataSource.extend(
/** @scope Todos.TaskDataSource.prototype */ {
// ..........................................................
// QUERY SUPPORT
//
fetch: function (store, query) {
if (query === Todos.TASKS_QUERY) {
SC.Request.getUrl('/tasks/tasks').json()
.notify(this, 'didFetchTasks', store, query)
.send();
return YES;
}
return NO ; // return YES if you handled the query
},
didFetchTasks: function (response, store, query) {
if (SC.ok(response)) {
store.loadRecords(Todos.Task, response.get('body'));
store.dataSourceDidFetchQuery(query);
} else {
store.dataSourceDidErrorQuery(query, response);
}
},
// ..........................................................
// RECORD SUPPORT
//
retrieveRecord: function (store, storeKey) {
if (SC.kindOf(store.recordTypeFor(storeKey), Todos.Task)) {
var url = "/tasks/tasks/"+store.idFor(storeKey);
SC.Request.getUrl(url).json()
.notify(this, 'didRetrieveTask', store, storeKey)
.send();
return YES;
} else {
return NO; // return YES if you handled the storeKey
}
},
didRetrieveTask: function (response, store, storeKey) {
if (SC.ok(response)) {
var dataHash = response.get('body');
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/tasks').json()
.notify(this, 'didCreateTask', store, storeKey)
.send(store.readDataHash(storeKey));
return YES;
} else {
return NO;
}
},
didCreateTask: function (response, store, storeKey) {
if (SC.ok(response)) {
store.dataSourceDidComplete(storeKey, null, response.header('Location').split('/').pop());
} else {
store.dataSourceDidError(storeKey, response);
}
},
updateRecord: function (store, storeKey) {
if (SC.kindOf(store.recordTypeFor(storeKey), Todos.Task)) {
SC.Request.putUrl("/tasks/tasks/"+store.idFor(storeKey)).json()
.notify(this, 'didUpdateTask', store, storeKey)
.send(store.readDataHash(storeKey));
return YES;
} else {
return NO;
}
},
didUpdateTask: function (response, store, storeKey) {
if (SC.ok(response)) {
store.dataSourceDidComplete(storeKey);
} else {
store.dataSourceDidError(storeKey);
}
},
destroyRecord: function (store, storeKey) {
if (SC.kindOf(store.recordTypeFor(storeKey), Todos.Task)) {
SC.Request.deleteUrl("/tasks/tasks/"+store.idFor(storeKey)).json()
.notify(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