Skip to content
Permalink
Browse files

Tutorial Part 4: Communicating with Google's APIs using Backbone.sync

  • Loading branch information...
Alex Young
Alex Young committed Dec 20, 2012
1 parent c1d5a2e commit fcd653ec6fa5916246e3f8b9b5f942f4be31d2e7
Showing with 50 additions and 1 deletion.
  1. +13 −1 app/js/app.js
  2. +8 −0 app/js/collections/tasklists.js
  3. +15 −0 app/js/gapi.js
  4. +7 −0 app/js/models/task.js
  5. +7 −0 app/js/models/tasklist.js
@@ -2,23 +2,35 @@ define([
'gapi'
, 'views/app'
, 'views/auth'
, 'collections/tasklists'
],

function(ApiManager, AppView, AuthView) {
function(ApiManager, AppView, AuthView, TaskLists) {
var App = function() {
this.views.app = new AppView();
this.views.app.render();

this.views.auth = new AuthView(this);
this.views.auth.render();

this.collections.lists = new TaskLists();

this.connectGapi();
};

App.prototype = {
views: {},
collections: {},
connectGapi: function() {
var self = this;
this.apiManager = new ApiManager(this);
this.apiManager.on('ready', function() {
self.collections.lists.fetch({ data: { userId: '@me' }, success: function(res) {
_.each(res.models, function(model) {
console.log(model.get('title'));
});
}});
});
}
};

@@ -0,0 +1,8 @@
define(['models/tasklist'], function(TaskList) {
var TaskLists = Backbone.Collection.extend({
model: TaskList
, url: 'tasklists'
});

return TaskLists;
});
@@ -34,6 +34,7 @@ define(['config'], function(config) {

app.views.auth.$el.hide();
$('#signed-in-container').show();
self.trigger('ready');
} else {
if (authResult && authResult.error) {
// TODO: Show error
@@ -87,9 +88,23 @@ define(['config'], function(config) {
break;

case 'read':
request = gapi.client.tasks[model.url].list(options.data);
Backbone.gapiRequest(request, method, model, options);
break;
}
};

Backbone.gapiRequest = function(request, method, model, options) {
var result;
request.execute(function(res) {
if (res.error) {
if (options.error) options.error(res);
} else if (options.success) {
result = res.items;
options.success(result, true, request);
}
});
};

return ApiManager;
});
@@ -0,0 +1,7 @@
define(function() {
var Task = Backbone.Model.extend({
url: 'tasks'
});

return Task;
});
@@ -0,0 +1,7 @@
define(function() {
var TaskList = Backbone.Model.extend({
url: 'tasklists'
});

return TaskList;
});

0 comments on commit fcd653e

Please sign in to comment.
You can’t perform that action at this time.