Transcript
Page 1: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

SOFEA Arquiteturas REST comBackbone e HTML5

Gabriel Zigolis / @zigolis

Page 2: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 3: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

WTF is SOFEA 

?

Page 4: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

S O F E AService Oriented Front End Architecture

Page 5: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

SOFEA“Proposes to remove all presentation

layer logic from the server to JavaScriptlogic on the client.”

thinserverarchitecture.com

Page 6: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Divisão de responsabilidades no desenvolvimentoBaixo acoplamento das camadasComunicação client/server através de verbos HTTPO client requisita o que e quando

O que nós ganhamos com isso?

Page 7: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

RESTin peace

Page 8: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

R E S T“Software architectural style consisting of a

coordinated set of architectural constraints appliedcomponents, conectors and data elements, within

a distributed hypermedia system.”

Roy Thomas Fielding

Page 9: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

CaracterísticasClient‑ServerStatelessCacheHTTP verbs: GET, POST, PUT, DELETE...

Page 10: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 11: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Backbone“Gives structure to web applications by providing

models, collections and views to consume API overa RESTful JSON interface.”

backbonejs.org

Page 12: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

CaracterísticasPoderosa LIB JavaScriptAdaptável, baseada no padrão MV*Moderninha, largamente usada em SPAMagrinha, apenas 6.5Kb

Page 13: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 14: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

HTML5 é a n...Tá de 

brinqueichon uite me,cara?

Page 15: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

HTML5 Rocks

Page 16: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

W3C

Page 17: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Material em PT

Page 18: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Collection

Page 19: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 20: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 21: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Wooow...What's happened?

Page 22: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Magic?

Page 23: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

WebSocket?

Page 24: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Fala para eles, Isabelle

Page 25: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Collection

Page 26: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 27: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 28: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Hands on

Page 29: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis
Page 30: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

JavaScript é terapia!

Mulinari, King of Xanxerê

Page 31: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

ListView

Page 32: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowCollectionvar FollowCollection = Backbone.Collection.extend({ url: '/api/following/', model: FollowModel});return FollowCollection;

Page 33: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowModelvar FollowModel = Backbone.Model.extend({ urlRoot: function() { return '/api/follow/' }});return FollowModel;

Page 34: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowViewvar FollowView = Backbone.View.extend({ initialize: function() { this.collection = new FollowCollection(); this.collection.fetch(); } ...});return FollowView;

Page 35: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowView @followListinitialize: function() { ... this.followList = new FollowListView({ 'collection': this.collection, 'el': this.$('.list‑view') });}

Page 36: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowListViewvar FollowListView = Backbone.View.extend({ template: _.template( $('#tmp‑list‑view').html() ),

initialize: function() { this.listenTo( this.collection, 'sync remove add', this.render ); }, ...});return FollowListView;

Page 37: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowListView @render...render: function() { this.$el.html(this.template({ 'collection': this.collection, 'view': this }));}

Page 38: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowButton

Page 39: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

ButtonModelvar ButtonModel = Backbone.Model.extend({ "defaults": { "follow": false }, urlRoot: function() { return '/api/follow/' }});return ButtonModel;

Page 40: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

ButtonViewvar ButtonView = Backbone.View.extend({ el: '.follow‑section', events: { 'click .follow' : 'follow' }, ...});return ButtonView;

Page 41: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

ButtonView @initialize...initialize: function() { this.model = new ButtonModel(); this.listenTo( this.model, "change", follow );},...

Page 42: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

ButtonView @follow...follow: function() { this.model.set({ follow: true }); this.model.save()

.done(function(data) { ...}

Page 43: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

ButtonView @follow....done(function(data) { model = new FollowModel(data);

Backbone.trigger( "u:follow", model ); Backbone.trigger( "u:followCount" );

this.following();});...

Page 44: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

Here is where the magic happens!

Page 45: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

FollowViewvar FollowView = Backbone.View.extend({ initialize: function(){ Backbone.on( 'u:follow', this.addToCollection, this ); }, addToCollection: function(model) { this.collection.add(model); }});return FollowView;

Page 46: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

...and the magic goes on and on

Page 47: SOFEA - Arquiteturas REST com Backbone & HTML5 by Gabriel Zigolis

That's all, folks.

e não esqueçam do ;