Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Enterprise and Mobile Web

Enterprise and Mobile Web

The mobile web is the intersection of mobile and cloud computing trends for many large companies. Fragmented device landscapes require a universal solution to their mobile pains, and apps built with tools like Backbone, HTML5, and backend as a service are uniquely positioned to provide that solution.

Avatar for Dave Wasmer

Dave Wasmer

August 01, 2013
Tweet

More Decks by Dave Wasmer

Other Decks in Technology

Transcript

  1. var BlogPost = Backbone.AssociatedModel.extend({ ! relations: [ { type: Backbone.Many,

    key: 'comments', relatedModel: BlogPostComment } ] ! });
  2. var BlogPost = Backbone.AssociatedModel.extend({ ! goto: function() { Router.navigate(_.result(this, ‘getResourceUrl’));

    }, ! getResourceUrl: function() { return “posts/” + this.id; }, ! // relations ... ! });
  3. var BaseModel = Backbone.AssociatedModel.extend({ ! goto: function() { Router.navigate(_.result(this, ‘getResourceUrl’));

    }, ! getResourceUrl: function() { throw new Error(‘no resource url given’); } ! }); ! var BlogPost = BaseModel.extend({ ! // relations ...
  4. var BlogPost = Backbone.AssociatedModel.extend({ ! // relations ... ! });

    ! var ResourceRoutingMixin = { goto: function() { Router.navigate(_.result(this, ‘getResourceUrl’)); }, getResourceUrl: function() { throw new Error(‘no resource url given’); } } _.extend(BlogPost.prototype, ResourceRoutingMixin);
  5. fetch: function(options) { if (options.useCache) { var transaction = db.transaction(this.name);

    var store = transaction.objectStore(this.name); var query = store.get(this.id); query.onsuccess = function(e) { this.set(query.result); options.success(this, null, options); } Backbone.Model.prototype.fetch.apply(this, arguments); } }
  6. fetch: function(options) { if (options.useCache) { var transaction = db.transaction(this.name);

    var store = transaction.objectStore(this.name); var query = store.get(this.id); query.onsuccess = function(e) { this.set(query.result); options.success(this, null, options); } Backbone.Model.prototype.fetch.apply(this, arguments); } }
  7. fetch: function(options) { if (options.useCache) { var transaction = db.transaction(this.name);

    var store = transaction.objectStore(this.name); var query = store.get(this.id); query.onsuccess = function(e) { this.set(query.result); options.success(this, null, options); } Backbone.Model.prototype.fetch.apply(this, arguments); } }
  8. fetch: function(options) { if (options.useCache) { var transaction = db.transaction(this.name);

    var store = transaction.objectStore(this.name); var query = store.get(this.id); query.onsuccess = function(e) { this.set(query.result); options.success(this, null, options); } Backbone.Model.prototype.fetch.apply(this, arguments); } }
  9. save: function(data, options) { this.set(data, options); if (!navigator.onLine) { var

    transaction = db.transaction(this.name, 'readwrite'); var store = transaction.objectStore(this.name); var insert = store.put(this.attributes); insert.onsuccess = options.success; } else { // write to server } }
  10. save: function(data, options) { this.set(data, options); if (!navigator.onLine) { var

    transaction = db.transaction(this.name, 'readwrite'); var store = transaction.objectStore(this.name); var insert = store.put(this.attributes); insert.onsuccess = options.success; ! var toSave = transaction.objectStore('to-save'); var tracker = toSave.put({id: this.id, name: this.name}); ! } else { // write to server } }
  11. save: function(data, options) { this.set(data, options); if (!navigator.onLine) { var

    transaction = db.transaction(this.name, 'readwrite'); var store = transaction.objectStore(this.name); var insert = store.put(this.attributes); insert.onsuccess = options.success; ! var toSave = transaction.objectStore('to-save'); var tracker = toSave.put({id: this.id, name: this.name}); tracker.onsuccess = ?  ! } else { // write to server } }
  12. success = _.after(options.success, 2);  ! var transaction = db.transaction(this.name,

    'readwrite'); var store = transaction.objectStore(this.name); var insert = store.put(this.attributes); insert.onsuccess = success; ! var toSave = transaction.objectStore('to-save'); var tracker = toSave.put({id: this.id, name: this.name}); tracker.onsuccess = success;
  13. var sync = function() { var toSave = db.transaction('to-save').objectStore('to-save') toSave.openCursor().onsuccess

    = function(event) { var cursor = event.target.result; if (cursor) { save(cursor.value); cursor.continue(); } else { console.log('all done!'); } } }
  14. var save = function(lookup) { var id = lookup.id, name

    = lookup.name; var store = db.transaction(name).objectStore(name); ! store.get(id).onsuccess = function(event) { record = event.target.result; fetch({name: name, record: record}) .then(function(serverCopy) { if (serverCopy.last_modified > record.last_modified) { resolveConflict({ server: serverCopy, client: record }); } }); } }