122
Saturday, June 22, 13

Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Embed Size (px)

DESCRIPTION

Slides from Colearn class with Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS When developing HTML5 application, your code is bound to get more complex over time. Employing design patterns like MVC are important for separating concerns and creating more modular and reusable code. Backbone.js is one of the more popular JavaScript frameworks to help you do this. Unfortunately, like most open source, the documentation is a work in progress. Don't worry, I'll walk you through a series of examples that build on each other as I show how the moving parts work together. We'll add the StackMob JavaScript SDK to extend backbone.js and persist your data in the cloud. We'll wrap up our session looking at how require.js allows you to break your backbone.js code into discreet modules during development. Once you are ready to deploy you can use require.js to build an optimized version of your javascript for production.

Citation preview

Page 1: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 2: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Sidney MaestrePlatform Evangelist

@SidneyAllenGitHub | Twitter

Saturday, June 22, 13

Page 3: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 4: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 5: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 6: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

WHAT

Saturday, June 22, 13

Page 7: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

WHY

Saturday, June 22, 13

Page 8: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

WHY

Saturday, June 22, 13

Page 9: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Controller

Model View

Saturday, June 22, 13

Page 10: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Controller

Model View

Saturday, June 22, 13

Page 11: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Controller

Model View

Saturday, June 22, 13

Page 12: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Controller

Model View

Saturday, June 22, 13

Page 13: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 14: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 15: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

Saturday, June 22, 13

Page 16: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

Saturday, June 22, 13

Page 17: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON01-model

Saturday, June 22, 13

Page 18: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

Wine = Backbone.Model.extend();

Saturday, June 22, 13

Page 19: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

Wine = Backbone.Model.extend();

Saturday, June 22, 13

Page 20: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

Wine = Backbone.Model.extend();

firstWine = new Wine({ winery : 'Clos Pegase', year : '2008', type : 'Chardonnay', size : '750ml',});

Saturday, June 22, 13

Page 21: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Saturday, June 22, 13

Page 22: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON02-collection

Saturday, June 22, 13

Page 23: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ model: Wine, url: "#"});

Saturday, June 22, 13

Page 24: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ model: Wine, url: "#"});

Saturday, June 22, 13

Page 25: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ model: Wine, url: "#"});

wines = new Wines([ {winery : "Robert Mondovi"}, {winery : "CakeBread"}]);

Saturday, June 22, 13

Page 26: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ model: Wine, url: "#"});

wines = new Wines([ {winery : "Robert Mondovi"}, {winery : "CakeBread"}]);

wines.each( function(model) { console.log(model.get('winery')); });

Saturday, June 22, 13

Page 27: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ model: Wine, url: "#"});

wines = new Wines([ {winery : "Robert Mondovi"}, {winery : "CakeBread"}]);

wines.each( function(model) { console.log(model.toJSON()); });

Saturday, June 22, 13

Page 28: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 29: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 30: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 31: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 32: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 33: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Datastore

Geo Queries

Facebook

Analytics

Load Balancing

Versioning

Push Notifications

CollaborationAccess

Controls Twitter

Amazon S3 User Authentication

Saturday, June 22, 13

Page 34: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 35: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Custom Code

Saturday, June 22, 13

Page 36: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

and talk to any API

Saturday, June 22, 13

Page 37: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

and talk to any API

Saturday, June 22, 13

Page 38: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Easy to use SDKs

Saturday, June 22, 13

Page 39: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Easy to use SDKs

Saturday, June 22, 13

Page 40: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 41: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 42: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ONwww.stackmob.com

Saturday, June 22, 13

Page 43: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

DASHBOARD

Saturday, June 22, 13

Page 44: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

CREATE APP

1

2

3Saturday, June 22, 13

Page 45: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COPY

Saturday, June 22, 13

Page 46: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON03-stackmob-model

Saturday, June 22, 13

Page 47: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

STACKMOB SDK

http://static.stackmob.com/js/stackmob-js-0.9.1-min.js"

Saturday, June 22, 13

Page 48: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

INITStackMob.init({    publicKey: "814004dd-a72a-4425-9d2e-63d21d76588e",    apiVersion: 0});

Saturday, June 22, 13

Page 49: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL var Wine = StackMob.Model.extend({ schemaName: "wine"});

Saturday, June 22, 13

Page 50: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL var Wine = StackMob.Model.extend({ schemaName: "wine"});

var wine = new Wine({name:‘Robert Mondavi});

wine.create({ success: function(model){

}});

Saturday, June 22, 13

Page 51: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL var Wine = StackMob.Model.extend({ schemaName: "wine"});

var wine = new Wine({name:‘Robert Mondavi});

wine.create({ success: function(model){

}});

Saturday, June 22, 13

Page 52: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON04-stackmob-collection

Saturday, June 22, 13

Page 53: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

var Wine = StackMob.Model.extend({ schemaName: "wine"}); var Wines = StackMob.Collection.extend({ model: Wine});

COLLECTION

Saturday, June 22, 13

Page 54: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

var Wine = StackMob.Model.extend({ schemaName: "wine"}); var Wines = StackMob.Collection.extend({ model: Wine});

var wines = new Wines();

wines.fetch({async: true});

COLLECTION

Saturday, June 22, 13

Page 55: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTIONvar Wine = StackMob.Model.extend({ schemaName: "wine"}); var Wines = StackMob.Collection.extend({ model: Wine});

var wines = new Wines();

wines.fetch({async: true});

console.log(wines.toJSON());

Saturday, June 22, 13

Page 56: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION var Wine = StackMob.Model.extend({ schemaName: "wine"});

var Wines = StackMob.Collection.extend({ model: Wine});

var wines = new Wines();

wines.fetch({async: true});

var wine = new Wine({name:‘Robert Mondavi});

wine.create({ success: function(model){ wines.add(model); }});

Saturday, June 22, 13

Page 57: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 58: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 59: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

Saturday, June 22, 13

Page 60: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON05-home-view

Saturday, June 22, 13

Page 61: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

HomeView = Backbone.View.extend({});

Saturday, June 22, 13

Page 62: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

HomeView = Backbone.View.extend({

render: function() { this.$el.append("<h1>Wine Cellar</h1>"); return this; }});

Saturday, June 22, 13

Page 63: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

HomeView = Backbone.View.extend({

initialize: function() { this.render(); },

render: function() {

this.$el.append("<h1>Wine Cellar</h1>"); return this; }});

Wine Cellar

Saturday, June 22, 13

Page 64: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

HomeView = Backbone.View.extend({ el: 'body', initialize: function() { this.render(); },

render: function() {

this.$el.append("<h1>Wine Cellar</h1>"); return this; }});

Wine Cellar

Saturday, June 22, 13

Page 65: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

HomeView = Backbone.View.extend({ el: 'body', initialize: function() { this.render(); },

render: function() {

this.$el.append("<h1>Wine Cellar</h1>"); return this; }});

Wine Cellar Wine Cellar Wine Cellar Wine Cellar Wine Cellar

Saturday, June 22, 13

Page 66: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

HomeView = Backbone.View.extend({ el: 'body', initialize: function() { this.render(); },

render: function() { this.$el.empty(); this.$el.append("<h1>Wine Cellar</h1>"); return this; }});

Wine Cellar

Saturday, June 22, 13

Page 67: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON06-list-view

Saturday, June 22, 13

Page 68: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

ListView = Backbone.View.extend({ tagName: 'ul',

render: function() { this.$el.empty(); this.$el.append("<li>Wine 1</li>"); this.$el.append("<li>Wine 2</li>"); return this; }});

Saturday, June 22, 13

Page 69: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Wine Cellar

VIEW

HomeView = Backbone.View.extend({ el: 'body', initialize: function() { this.render(); },

render: function() { this.empty(); this.$el.append("<h1>Wine Cellar</h1>");

this.listView = new ListView(); this.$el.append(this.listView.render().el);

return this; }});

Saturday, June 22, 13

Page 70: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON07-basic-template

Saturday, June 22, 13

Page 71: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

TEMPLATE<script type="text/template" id="listTemplate"> <li><%= winery %></li></script>

Saturday, June 22, 13

Page 72: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

TEMPLATE<script type="text/template" id="listTemplate"> <li><%= winery %></li></script>

ListView = Backbone.View.extend({ tagName: 'ul', initialize: function() { this.template = _.template($('#listTemplate').html()); },

...

});

Saturday, June 22, 13

Page 73: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

TEMPLATE<script type="text/template" id="listTemplate"> <li><%= value %></li></script>ListView = Backbone.View.extend({ tagName: 'ul', initialize: function() { this.template = _.template($('#listTemplate').html()); wines.bind('all', this.render,this); this.render(); },

render: function() { ... this.$el.append(this.template({value : "Cakebread"}));

return this; }});

Saturday, June 22, 13

Page 74: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON08-collection-template

Saturday, June 22, 13

Page 75: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

TEMPLATE<script type="text/template" id="listTemplate"> <li><%= value %></li></script>

ListView = Backbone.View.extend({ ... render: function() { var el = this.$el, template = this.template;

el.empty(); wines.each(function(wine){ el.append(template( wine.toJSON() )); });

return this; }});

Saturday, June 22, 13

Page 76: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTER

Saturday, June 22, 13

Page 77: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTER

Saturday, June 22, 13

Page 78: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON09-basic-router

Saturday, June 22, 13

Page 79: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERAppRouter = Backbone.Router.extend({ routes:{ "":"home", "add":"add" },

home:function () { new HomeView(); }, add:function () { new AddView(); }});

Saturday, June 22, 13

Page 80: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERvar app = (function($){

...

var initialize = function() { wineApp = new AppRouter(); Backbone.history.start(); }

return { initialize : initialize }

}(jQuery));

$(document).ready(function () { app.initialize();});

Saturday, June 22, 13

Page 81: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON10-adv-router

Saturday, June 22, 13

Page 82: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERAppRouter = Backbone.Router.extend({ routes:{ "":"home", "add":"add" },

initialize:function(options) { this.collection = options.collection; },

home:function () { new HomeView({collection:this.collection}); }, add:function () { new AddView({collection:this.collection}); }});

Saturday, June 22, 13

Page 83: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERvar initialize = function() { var wines = new Wines(); wines.fetch({async:true});

wineApp = new AppRouter({collection : wines}); Backbone.history.start();}

Saturday, June 22, 13

Page 84: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON11-events

Saturday, June 22, 13

Page 85: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

EVENTSAddView = Backbone.View.extend({ events: { "click #addBtn": "add", ... },

... add: function(e) { // do something... return this; }});

Saturday, June 22, 13

Page 86: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ADDadd: function(e) { var collection = this.collection;

var wine = new Wine({winery:$('#winery').val() });

wine.create({ success: function(model){ } }); return this;}

Saturday, June 22, 13

Page 87: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ADDadd: function(e) { var collection = this.collection;

var wine = new Wine({winery:$('#winery').val() });

wine.create({ success: function(model){ } }); return this;}

Saturday, June 22, 13

Page 88: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ADDadd: function(e) { var collection = this.collection;

var wine = new Wine({winery:$('#winery').val() });

wine.create({ success: function(model){ collection.add(model); } }); return this;}

Saturday, June 22, 13

Page 89: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ADDadd: function(e) { var collection = this.collection;

var wine = new Wine({winery:$('#winery').val() });

wine.create({ success: function(model){ collection.add(model); } }); return this;}

Saturday, June 22, 13

Page 90: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ON12-update

Saturday, June 22, 13

Page 91: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

TEMPLATE<script type="text/template" id="listTemplate"> <li><a href="#update/<%= wine_id %>"><%= winery %></a></li></script>

Saturday, June 22, 13

Page 92: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERAppRouter = Backbone.Router.extend({ routes:{ "":"home", "add":"add", "update/:id":"update" },

...

update:function(e) { model = this.collection.get(e); new UpdateView({model: model}); }});

Saturday, June 22, 13

Page 93: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEWvar UpdateView = Backbone.View.extend({ ... initialize: function() { this.model = this.options.model; },

save: function(e) { this.model.save({winery:$('#winery').val()}, { success: function(model) { } }); return this; }});

Saturday, June 22, 13

Page 94: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEWvar UpdateView = Backbone.View.extend({ ... initialize: function() { this.model = this.options.model; },

save: function(e) { this.model.save({winery:$('#winery').val()}, { success: function(model) { } }); return this; }});

Saturday, June 22, 13

Page 95: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 96: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 97: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

STRUCTURE

Saturday, June 22, 13

Page 98: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

Wine = Backbone.Model.extend();

Saturday, June 22, 13

Page 99: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MODEL

define([ 'backbone'], function(Backbone) {

var WineModel = Backbone.Model.extend(); return WineModel;

});

Saturday, June 22, 13

Page 100: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ Model: Wine, url: "#"});

Saturday, June 22, 13

Page 101: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

COLLECTION

define([ 'backbone', 'models/wine/Model'], function(Backbone,Model){

var WineCollection = Backbone.Collection.extend({ Model: Model, url: '#' });

return WineCollection;});

Saturday, June 22, 13

Page 102: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

ListView = Backbone.View.extend({ ... render: function() { var el = this.$el, template = this.template;

el.empty(); wines.each(function(wine){ el.append(template( wine.toJSON() )); });

return this; }});

Saturday, June 22, 13

Page 103: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

VIEW

define([ 'jquery', 'underscore', 'backbone', 'text!templates/wine/WineListTemplate.html'], function($, _, Backbone,WineListTemplate){

var WineListView = Backbone.View.extend({

...

});

return WineListView;});

Saturday, June 22, 13

Page 104: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ONrequire

Saturday, June 22, 13

Page 105: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

BOOTSTRAP<script data-main="js/main" src="js/libs/require/require.js"></script>

Saturday, June 22, 13

Page 106: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MAIN.JSrequire.config({ baseUrl: "/js/", paths: { jquery: 'libs/jquery/jquery-1.8.2', underscore: 'libs/underscore/underscore-1.4.4', backbone: 'libs/backbone/backbone-1.0.0', templates: '../templates', app: 'app' },

shim: { underscore: { exports: '_' }, backbone: { deps: ["underscore", "jquery"], exports: "Backbone" } }});

Saturday, June 22, 13

Page 107: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

MAIN.JSrequire(['jquery','app'], function( $, App ){

$(function(){ App.initialize(); });

});

Saturday, June 22, 13

Page 108: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

APP.JSdefine([ 'jquery', 'underscore', 'backbone', 'router' // Request router.js], function($, _, Backbone, Router){

var initialize = function() { Router.initialize(); };

return { initialize: initialize };});

Saturday, June 22, 13

Page 109: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERdefine([ 'jquery', 'underscore', 'backbone', 'views/home/HomeView', 'views/wine/AddView', 'collections/wine/WineCollection'], function($, _,Backbone, HomeView, AddView, UpdateView, Wines) { ...

var initialize = function(){ var wines = new Wines(); var app_router = new AppRouter({collection: wines});

Backbone.history.start(); }; return { initialize: initialize };});

Saturday, June 22, 13

Page 110: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

ROUTERAppRouter = Backbone.Router.extend({ initialize: function(options) { this.collection = options.collection; },

routes:{ "":"home", "add":"add" },

home:function () { this.changePage( new HomeView({collection : this.collection}) ); },

add:function () { this.changePage( new AddView({collection : this.collection, router : this}) ); },

});

Saturday, June 22, 13

Page 111: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

R.JS

Saturday, June 22, 13

Page 112: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

BUILD.JS({ appDir: "../", baseUrl: "js", dir: "../../appdirectory-build", paths: { ... },

shim: { ... }, modules: [ { name: "main" } ]})

Saturday, June 22, 13

Page 113: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

BUILDnode r.js -o build.js optimize=none

347k

Saturday, June 22, 13

Page 114: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

OPTIMIZEnode r.js -o build.js

134k

Saturday, June 22, 13

Page 115: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 116: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ONjqm

Saturday, June 22, 13

Page 117: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ONjqm-template

Saturday, June 22, 13

Page 118: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ONdevelopment

Saturday, June 22, 13

Page 119: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

HANDS ONmywine

Saturday, June 22, 13

Page 120: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Saturday, June 22, 13

Page 121: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Resourcesbit.ly/freebackbonejs

github.com/SidneyAllen

developer.stackmob.com

Saturday, June 22, 13

Page 122: Sid Maestre: Building APPS with BACKBONE.JS & REQUIRE.JS

Q&ASaturday, June 22, 13