Spine js & creating non blocking user interfaces

27
Spine.js & creating non-blocking user interfaces Hjortur Hilmarsson @hjortureh

Transcript of Spine js & creating non blocking user interfaces

Page 1: Spine js & creating non blocking user interfaces

Spine.js & creating non-blocking user interfaces

Hjortur Hilmarsson@hjortureh

Page 2: Spine js & creating non blocking user interfaces

Agenda

● Why MVC ?● Spine.js● Async UIs● Tips & Tricks● QnA

Page 3: Spine js & creating non blocking user interfaces

Javascript MVC

Page 4: Spine js & creating non blocking user interfaces

“I wan't better UX”The user

Why MVC ?

Page 5: Spine js & creating non blocking user interfaces

“Can it be done in HTML5 ?”The Boss

Why MVC ?

Page 6: Spine js & creating non blocking user interfaces

“So much Javascript, no structure & always solving the same

problems”The Developer

Why MVC ?

Page 7: Spine js & creating non blocking user interfaces

Common problems

● Structure ● Events● Routing● RESTful sync● Local storage

Page 8: Spine js & creating non blocking user interfaces

MVC Frameworks

Source: https://github.com/addyosmani/todomvc

● Backbone.js● Ember.js (SproutCore 2.0)● Spine.js● JavaScriptMVC● Sammy.js● YUILibrary● KnockoutJS (MVVM)● Knockback● AngularJS● Broke.js● Fidel.js● ExtJS

Page 9: Spine js & creating non blocking user interfaces

Backbone & SpineLightweight MVC frameworks

Page 10: Spine js & creating non blocking user interfaces

Spine.js

● Inspire by Backbone.js● Written in Coffee Script● Async UIs● Very light ( 2k )● Includes

○ Classes○ Models○ Controllers○ Routes○ Events

Page 11: Spine js & creating non blocking user interfaces

Backbone vs SpineViews vs. Controllers

Model/Collection vs. Model

Page 12: Spine js & creating non blocking user interfaces

Backbone model

var ContactModel = Backbone.Model.extend({});

var ContactCollection = Backbone.Collection.extend({model: ContactModel,url: "contacts"});

// Usage

var contacts = new ContactCollection();var contact = new ContactModel({ name: "Sven Svensson", email: "[email protected]" });

contacts.create( contact );

Page 13: Spine js & creating non blocking user interfaces

Spine.js model

var ContactModel = Spine.Model.sub();ContactModel.configure("Contact", "name", "email");ContactModel.extend( Spine.Model.Ajax );

// Usage

var contact = new ContactModel( { name: "Sven Svensson", email: "[email protected]" });

contact.save();

Page 14: Spine js & creating non blocking user interfaces

Spine.js model in Coffee Script

class ContactModel extends Spine.Model @configure "Contact", "name", "email" @extend "Contact", "name", "email"

// Usage

contact = new ContactModel( name: "Sven Svensson", email: "[email protected]")

contact.save()

Page 15: Spine js & creating non blocking user interfaces
Page 16: Spine js & creating non blocking user interfaces

Backbone viewvar ContactView = Backbone.View.extend({

events: { "click li": "openContact" },

initialize: function() {contacts.bind( "reset", reset, this );contacts.fetch();},reset: function() { this.contactList = this.$("#contact-list"); ... },

openContact: function() { ... },

});

// Usagenew ContactView({ el: $("#view").get(0) });

Page 17: Spine js & creating non blocking user interfaces

Spine.js controllervar ContactView = Spine.Controller.create({

events: { "click li": "openContact" },

elements: { "#contact-list": "contactList" },

init: function() {ContactModel.bind( "refresh", refresh );ContactModel.fetch();},

openContact: function() { ... },refresh: function() { ... },

});

// Usagenew ContactView({ el: $("#view") });

Page 18: Spine js & creating non blocking user interfaces

Async UIs

Page 19: Spine js & creating non blocking user interfaces

Async UIs 

Backbone Spine

save() save()

validate() validate()

ajax() save callback()

save callback() ajax()

Page 20: Spine js & creating non blocking user interfaces

Contact Us - Examples

Page 21: Spine js & creating non blocking user interfaces

Does not work everywhere

● Transaction● Login ● Need for server-side validation

Notes: ● Spine.js also has ajaxSuccess & ajaxError events● Works for CUD, not Read

Page 22: Spine js & creating non blocking user interfaces

Don't believe me ?

■Amazon: 100 ms of extra load time caused a 1% drop in sales (source: Greg Linden, Amazon).

■Google: 500 ms of extra load time caused 20% fewer searches (source: Marrissa Mayer, Google).

■Yahoo!: 400 ms of extra load time caused a 5–9% increase in the number of people who clicked “back” before the page even loaded (source: Nicole Sullivan, Yahoo!).

Page 23: Spine js & creating non blocking user interfaces
Page 24: Spine js & creating non blocking user interfaces

What about read ? - Examples

Page 25: Spine js & creating non blocking user interfaces

Bonus

● Read Docs & Code● Check TodoMVC● Use Jasmine / Sinon.js for testing

Page 26: Spine js & creating non blocking user interfaces

QnA

Page 27: Spine js & creating non blocking user interfaces

Thanks!

@hjortureh