21
Structuring web applications with Backbone.js Diego Cardozo github.com/diegocard/backbone-presentation

Structuring web applications with Backbone.js

Embed Size (px)

DESCRIPTION

Talk on how to structure web applications with Backbonejs, presented at a local JavaScript meetup.

Citation preview

Page 1: Structuring web applications with Backbone.js

StructuringwebapplicationswithBackbone.js

DiegoCardozo

github.com/diegocard/backbone-presentation

Page 2: Structuring web applications with Backbone.js

GoalsThispresentationisn'tonlyaBackbonetutorialWe'llfocusoncomplexclientdesignUsingBackboneasthemaintoolMostconceptswon'tbetool-specificCanbeappliedtoothertoolslikeAngularorKnockoutWe'lllearnBackbonethroughexamples

Page 3: Structuring web applications with Backbone.js

WhatdoIwantyoutolearn?UsefulconceptsforcomplexwebclientdesignBasicBackboneknowledgeMotivationtokeeplearning

ForthosewhoalreadyknowBackbone

GoodpracticesCombinationwithothertools

Page 4: Structuring web applications with Backbone.js

Agenda1. Introduction2. Architecture3. Example4. Backbonecomponents5. Structuringawebapplication

Page 5: Structuring web applications with Backbone.js

Introduction(1)WebclientshavebetterresourceseverydayWecannowbuildsmartclientsButcomplexapplicationswithjQuery...

ArehardtobuildLackstructureDonotfavorreutilizationCreatingyourownstructureisreinventingthewheel

Page 6: Structuring web applications with Backbone.js

Introduction(2)

Page 7: Structuring web applications with Backbone.js

Introduction(3)Backbonegivesus

Structureforourclient-sideJavaScriptcodeSeveralutilities

Basically,itisaMV*frameworkWeorganizecodeindifferentcomponents

ModelsCollectionsViewsTemplatesRouters

Page 8: Structuring web applications with Backbone.js

Architecture(1)

Page 9: Structuring web applications with Backbone.js

Architecture(2)Advantages

MaintainabilityLoaddistributionQuickerstarttothedevelopmentprocessUIisonlyanotherclientGreatfortestingPerfectforcombiningwithmobileapps

Page 10: Structuring web applications with Backbone.js

Examplegithub.com/diegocard/backbone-presentation/demo

Page 11: Structuring web applications with Backbone.js

Components(1)Model

varUser=Backbone.Model.extend({urlRoot:'/users'});

Page 12: Structuring web applications with Backbone.js

Components(2)Collection

varUsers=Backbone.Collection.extend({url:'/users'});

Page 13: Structuring web applications with Backbone.js

Components(3)View

varUserListView=Backbone.View.extend({el:'.page',render:function(){varthat=this;varusers=newUsers();users.fetch({success:function(users){vartemplate=_.template($('#user-list-template').html(),{users:users.models});that.$el.html(template);}})}});

Page 14: Structuring web applications with Backbone.js

Components(4)Eventhandling

varUserEditView=Backbone.View.extend({el:'.page',events:{'submit.edit-user-form':'saveUser','click.delete':'deleteUser'},saveUser:function(ev){varuserDetails=$(ev.currentTarget).serializeObject();varuser=newUser();user.save(userDetails,{success:function(user){router.navigate('',{trigger:true});}});}});

Page 15: Structuring web applications with Backbone.js

Components(5)Template

<scripttype="text/template"id="user-list-template"><ahref="#/new"class="btnbtn-primary">New</a><hr/><tableclass="tablestriped"><thead><tr><th>FirstName</th><th>LastName</th><th>Age</th><th></th></tr></thead><tbody><%_.each(users,function(user){%><tr><td><%=htmlEncode(user.get('firstname'))%></td><td><%=htmlEncode(user.get('lastname'))%></td><td><%=htmlEncode(user.get('age'))%></td><td><aclass="btn"href="#/edit/<%=user.id%>">Edit</a></td></tr><%});%></tbody></table></script>

Page 16: Structuring web applications with Backbone.js

Components(6)Router

varRouter=Backbone.Router.extend({routes:{"":"home","edit/:id":"edit","new":"edit",}});

Page 17: Structuring web applications with Backbone.js

Structure(1)Usingbackbonedoesn'tguaranteegoodpracticesWeneedtoorganizeandmodularizeourapplicationWecanuseRequire.jstoachievethisIfoundagreatexampleat:

backbonetutorials.com/organizing-backbone-using-modules

Page 18: Structuring web applications with Backbone.js

Structure(2)Suggestedstructure

Root├──imgs├──css│└──style.css├──templates│├──projects││├──list.html││└──edit.html│└──users│├──list.html│└──edit.html├──js│├──libs││├──jquery│││├──jquery.min.js││├──backbone│││├──backbone.min.js││└──underscore│││├──underscore.min.js│├──models││├──users.js

Page 19: Structuring web applications with Backbone.js

Structure(3)Example:Proyectlist

define(['jquery','underscore','backbone',//Usingthetext!pluginforRequire.js,//wecanloadtemplatesasplaintext'text!templates/project/list.html'],function($,_,Backbone,projectListTemplate){varProjectListView=Backbone.View.extend({el:$('#container'),render:function(){//Compilethetemplatewithunderscore//andaddthetemplatetotheviewelementvardata={};varcompiledTemplate=_.template(projectListTemplate,data);this.$el.append(compiledTemplate);}});returnProjectListView;//Returntheview});

Page 20: Structuring web applications with Backbone.js

Resources

backbonejs.orgbackbonetutorials.comaddyosmani.github.io/backbone-fundamentalsgithub.com/diegocard/backbone-presentation

Page 21: Structuring web applications with Backbone.js

¿Questions?