JSGeneve - Backbone.js

160

description

Introduction to Backbone.js

Transcript of JSGeneve - Backbone.js

Page 1: JSGeneve - Backbone.js
Page 2: JSGeneve - Backbone.js
Page 3: JSGeneve - Backbone.js
Page 4: JSGeneve - Backbone.js
Page 5: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 6: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 7: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 8: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 9: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 10: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 11: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 12: JSGeneve - Backbone.js

var Animal, myAnimal;

Animal = function (name) { this.name = name || 'Unknown';}

Animal.prototype = { say: function () { console.log( 'Hi, my name is ' + this.name + '!' ); }}

myAnimal = new Animal('Bello');myAnimal.say(); // Hi, my name is Bello!

Page 13: JSGeneve - Backbone.js
Page 14: JSGeneve - Backbone.js
Page 15: JSGeneve - Backbone.js

var Animal = Backbone.Model.extend({ say: function () {}});

var Dog = Animal.extend({ bark: function () {}});

Page 16: JSGeneve - Backbone.js

var Animal = Backbone.Model.extend({ say: function () {}});

var Dog = Animal.extend({ bark: function () {}});

Page 17: JSGeneve - Backbone.js

var Animal = Backbone.Model.extend({ say: function () {}});

var Dog = Animal.extend({ bark: function () {}});

Page 18: JSGeneve - Backbone.js

var Animal = Backbone.Model.extend({ say: function () {}});

var Dog = Animal.extend({ bark: function () {}});

Page 19: JSGeneve - Backbone.js

var Animal = Backbone.Model.extend({ say: function () {}});

var Dog = Animal.extend({ bark: function () {}});

Page 20: JSGeneve - Backbone.js

var Animal = Backbone.Model.extend({ say: function () {}});

var Dog = Animal.extend({ bark: function () {}});

Page 21: JSGeneve - Backbone.js

var Dog = Animal.extend({ bark: function () {}});

var myDog = new Dog({ name: 'Bello'});

Page 22: JSGeneve - Backbone.js

var Dog = Animal.extend({ bark: function () {}});

var myDog = new Dog({ name: 'Bello'});

Page 23: JSGeneve - Backbone.js

// The self-propagating extend function that Backbone classes use.var extend = function (protoProps, classProps) { var child = inherits(this, protoProps, classProps); child.extend = this.extend; return child;};

// Set up inheritance for the model, collection, and view.Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;

Page 24: JSGeneve - Backbone.js

// The self-propagating extend function that Backbone classes use.var extend = function (protoProps, classProps) { var child = inherits(this, protoProps, classProps); child.extend = this.extend; return child;};

// Set up inheritance for the model, collection, and view.Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;

Page 25: JSGeneve - Backbone.js

// The self-propagating extend function that Backbone classes use.var extend = function (protoProps, classProps) { var child = inherits(this, protoProps, classProps); child.extend = this.extend; return child;};

// Set up inheritance for the model, collection, and view.Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;

Page 26: JSGeneve - Backbone.js

// The self-propagating extend function that Backbone classes use.var extend = function (protoProps, classProps) { var child = inherits(this, protoProps, classProps); child.extend = this.extend; return child;};

// Set up inheritance for the model, collection, and view.Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;

Page 27: JSGeneve - Backbone.js

// The self-propagating extend function that Backbone classes use.var extend = function (protoProps, classProps) { var child = inherits(this, protoProps, classProps); child.extend = this.extend; return child;};

// Set up inheritance for the model, collection, and view.Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;

Page 28: JSGeneve - Backbone.js
Page 29: JSGeneve - Backbone.js
Page 30: JSGeneve - Backbone.js
Page 31: JSGeneve - Backbone.js
Page 32: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 33: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 34: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 35: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 36: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 37: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 38: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 39: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 40: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 41: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({

routes: { '': 'index', 'q?:query': 'query', 'show/:id': 'show' },

index: function () {}, query: function (queryString) {}, show: function (id) {}

});

Page 42: JSGeneve - Backbone.js

Backbone.Router.extend({ routes: { /* matches search/pierre/zurich */ 'search/:who/:where': 'serach', /* matches download/avatar/@shvi/large */ 'download/*path': 'download' } search: function (who, where) {}, download: function (path) {}});

Page 43: JSGeneve - Backbone.js

Backbone.Router.extend({ routes: { /* matches search/pierre/zurich */ 'search/:who/:where': 'serach', /* matches download/avatar/@shvi/large */ 'download/*path': 'download' } search: function (who, where) {}, download: function (path) {}});

Page 44: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({ // …});new AppRouter();Backbone.history.start();

Page 45: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({ // …});new AppRouter();Backbone.history.start();

Page 46: JSGeneve - Backbone.js

var AppRouter = Backbone.Router.extend({ // …});new AppRouter();Backbone.history.start();

Page 48: JSGeneve - Backbone.js
Page 49: JSGeneve - Backbone.js
Page 50: JSGeneve - Backbone.js

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

var newComment = new Comment({ name: 'Pierre Spring', email: '[email protected]', text: 'Hello World'});

Page 51: JSGeneve - Backbone.js

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

var newComment = new Comment({ name: 'Pierre Spring', email: '[email protected]', text: 'Hello World'});

Page 52: JSGeneve - Backbone.js

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

var newComment = new Comment({ name: 'Pierre Spring', email: '[email protected]', text: 'Hello World'});

Page 53: JSGeneve - Backbone.js

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

var newComment = new Comment({ name: 'Pierre Spring', email: '[email protected]', text: 'Hello World'});

Page 54: JSGeneve - Backbone.js

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

var newComment = new Comment({ name: 'Pierre Spring', email: '[email protected]', text: 'Hello World'});

Page 55: JSGeneve - Backbone.js

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

var newComment = new Comment({ name: 'Pierre Spring', email: '[email protected]', text: 'Hello World'});

Page 56: JSGeneve - Backbone.js

var newComment = new Comment({ text: 'Hello World'});

newComment.set({ name: 'Pierre Spring', email: '[email protected]'});

newComment.get('name'); // Pierre Spring

Page 57: JSGeneve - Backbone.js

var newComment = new Comment({ text: 'Hello World'});

newComment.set({ name: 'Pierre Spring', email: '[email protected]'});

newComment.get('name'); // Pierre Spring

Page 58: JSGeneve - Backbone.js

var newComment = new Comment({ text: 'Hello World'});

newComment.set({ name: 'Pierre Spring', email: '[email protected]'});

newComment.get('name'); // Pierre Spring

Page 59: JSGeneve - Backbone.js

var newComment = new Comment({ text: 'Hello World'});

newComment.set({ name: 'Pierre Spring', email: '[email protected]'});

newComment.get('name'); // Pierre Spring

Page 60: JSGeneve - Backbone.js

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

var newComment = new Comment({ /* … */ });

newComment.save();

Page 61: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ url: '/api/comment/'});

var newComment = new Comment({ /* … */ });

newComment.save();

Page 62: JSGeneve - Backbone.js
Page 63: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment")});

var newComment = new Comment({ /* … */ });

newComment.save();

Page 64: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { }});

var newComment = new Comment({ /* … */ });

newComment.save();

Page 65: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }});

var newComment = new Comment({ /* … */ });

newComment.save();

Page 66: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }});

var newComment = new Comment({ /* … */ });

newComment.save();

Page 67: JSGeneve - Backbone.js
Page 68: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }});

var newComment = new Comment({ /* … */ });

newComment.save();

Page 69: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }, getDisplayDate: function () { var d = this.get('date'); return moment(d).fromNow(); }});

Page 70: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }, getDisplayDate: function () { var d = this.get('date'); return moment(d).fromNow(); }});

Page 71: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }, getDisplayDate: function () { var d = this.get('date'); return moment(d).fromNow(); }});

Page 72: JSGeneve - Backbone.js

var Comment = Backbone.Model.extend({ localStorage: new Store("comment"), initialize: function (options) { if (!options.date) { this.set({ date: moment().toString() }); } }, getDisplayDate: function () { var d = this.get('date'); return moment(d).fromNow(); }});

Page 73: JSGeneve - Backbone.js

var newComment = new Comment({ /* … */ });

newComment.getDisplayDate();// a few seconds ago

Page 74: JSGeneve - Backbone.js

Backbone.Collection.extend({ model: CommentModel, localStorage: new Store("comment")});

Page 75: JSGeneve - Backbone.js

Backbone.Collection.extend({ model: CommentModel, localStorage: new Store("comment")});

Page 76: JSGeneve - Backbone.js

Backbone.Collection.extend({ model: CommentModel, localStorage: new Store("comment")});

Page 77: JSGeneve - Backbone.js

Backbone.Collection.extend({ model: CommentModel, localStorage: new Store("comment")});

Page 78: JSGeneve - Backbone.js

Backbone.Collection.extend({ model: CommentModel, localStorage: new Store("comment")});

Page 79: JSGeneve - Backbone.js

var CommentCollection = Backbone.Collection.extend({ /* … */});

var commentCollection = new CommentCollection();

commentCollection.fetch();

commentCollection.each(function (commentModel) { // do s.th.});

Page 80: JSGeneve - Backbone.js

var CommentCollection = Backbone.Collection.extend({ /* … */});

var commentCollection = new CommentCollection();

commentCollection.fetch();

commentCollection.each(function (commentModel) { // do s.th.});

Page 81: JSGeneve - Backbone.js

var CommentCollection = Backbone.Collection.extend({ /* … */});

var commentCollection = new CommentCollection();

commentCollection.fetch();

commentCollection.each(function (commentModel) { // do s.th.});

Page 82: JSGeneve - Backbone.js

var CommentCollection = Backbone.Collection.extend({ /* … */});

var commentCollection = new CommentCollection();

commentCollection.fetch();

commentCollection.each(function (commentModel) { // do s.th.});

Page 83: JSGeneve - Backbone.js
Page 84: JSGeneve - Backbone.js

var CommentCollection = Backbone.Collection.extend({ /* … */});

var commentCollection = new CommentCollection();

commentCollection.fetch();

commentCollection.each(function (commentModel) { // do s.th.});

Page 85: JSGeneve - Backbone.js

var newComment = new Comment({ /* … */ });newComment.save();commentCollection.add(newComment);

Page 86: JSGeneve - Backbone.js

var newComment = new Comment({ /* … */ });newComment.save();commentCollection.add(newComment);

Page 87: JSGeneve - Backbone.js

var newComment = new Comment({ /* … */ });newComment.save();commentCollection.add(newComment);

Page 88: JSGeneve - Backbone.js

var newComment = new Comment({ /* … */ });newComment.save();commentCollection.add(newComment);

Page 89: JSGeneve - Backbone.js

var newComment = new Comment({ /* … */ });newComment.save();commentCollection.add(newComment);

var newComment = commentCollection.create({ /* model attributes */});

Page 90: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 91: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 92: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 93: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 94: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 95: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 96: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 97: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 98: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 99: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 100: JSGeneve - Backbone.js

newComment.bind( 'change', function (model) {});newComment.bind( 'change:name', function (model, attribute) {});commentCollection.bind( 'reset', function (commentCollection) {});commentCollection.bind( 'add', function (commentModel) {});

Page 101: JSGeneve - Backbone.js
Page 102: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({});var commentView = new CommentView();console.log(commentView.el);// <div></div>

Page 103: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({});var commentView = new CommentView();console.log(commentView.el);// <div></div>

Page 104: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({});var commentView = new CommentView();console.log(commentView.el);// <div></div>

Page 105: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({});var commentView = new CommentView();console.log(commentView.el);// <div></div>

Page 106: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({});var commentView = new CommentView();console.log(commentView.el);// <div></div>

Page 107: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ tagName: 'p', className: 'comment-list', id: 'container',});var commentView = new CommentView();console.log(commentView.el);// <p id= "container" class= "comment-list"/>

Page 108: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ tagName: 'p', className: 'comment-list', id: 'container',});var commentView = new CommentView();console.log(commentView.el);// <p id= "container" class= "comment-list"/>

Page 109: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ tagName: 'p', className: 'comment-list', id: 'container',});var commentView = new CommentView();console.log(commentView.el);// <p id= "container" class= "comment-list"/>

Page 110: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ tagName: 'p', className: 'comment-list', id: 'container',});var commentView = new CommentView();console.log(commentView.el);// <p id= "container" class= "comment-list"/>

Page 111: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ el: $('#comment-list')});var commentView = new CommentView();console.log(commentView.el);

Page 112: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 113: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 114: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 115: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 116: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 117: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 118: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 119: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 120: JSGeneve - Backbone.js

var commentCollection = new commentCollection();

var commentView = new CommentView({ commentCollection: commentCollection});

Page 121: JSGeneve - Backbone.js

var commentCollection = new commentCollection();

var commentView = new CommentView({ commentCollection: commentCollection});

Page 122: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 123: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 124: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 125: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 126: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 127: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 128: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 129: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 130: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 131: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 132: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 133: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 134: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 135: JSGeneve - Backbone.js

var CommentView = Backbone.View.extend({ initialize: function (options) { this.listRowTemplate = _.template($('#listRowTemplate').html()); options.commentCollection.bind('reset', this.renderRows, this); options.commentCollection.bind('add', this.renderRow, this); },

renderRows: function (commentCollection) { commentCollection.each(function (commentModel) { this.renderRow(commentModel); }, this); },

renderRow: function (commentModel) { $(this.el).prepend( this.listRowTemplate({ comment: commentModel.toJSON() }) ); }});

Page 136: JSGeneve - Backbone.js

var commentCollection = new commentCollection();

var commentView = new CommentView({ commentCollection: commentCollection});

$('.list-view').html(commentView.el);

commentCollection.fetch();

Page 137: JSGeneve - Backbone.js

var commentCollection = new commentCollection();

var commentView = new CommentView({ commentCollection: commentCollection});

$('.list-view').html(commentView.el);

commentCollection.fetch();

Page 138: JSGeneve - Backbone.js

var commentCollection = new commentCollection();

var commentView = new CommentView({ commentCollection: commentCollection});

$('.list-view').html(commentView.el);

commentCollection.fetch();

Page 139: JSGeneve - Backbone.js

var commentCollection = new commentCollection();

var commentView = new CommentView({ commentCollection: commentCollection});

$('.list-view').html(commentView.el);

commentCollection.fetch();

Page 140: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 141: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 142: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 143: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 144: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 145: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 146: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 147: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 148: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 149: JSGeneve - Backbone.js

var CommentFormView = Backbone.View.extend({ events: { 'submit form': 'newComment' }, initialize: function (options) { /* … */ }, render: function () { /* … */ },

newComment: function (e) { var options, comment; e.preventDefault();

options = {}; _.each( this.$('form').serializeArray(), function (field) { options[field.name] = field.value; } );

this.commentCollection.create(options);

this.$('input, textarea').val(''); }});

Page 150: JSGeneve - Backbone.js
Page 152: JSGeneve - Backbone.js
Page 153: JSGeneve - Backbone.js
Page 154: JSGeneve - Backbone.js
Page 155: JSGeneve - Backbone.js
Page 156: JSGeneve - Backbone.js
Page 157: JSGeneve - Backbone.js
Page 158: JSGeneve - Backbone.js
Page 159: JSGeneve - Backbone.js
Page 160: JSGeneve - Backbone.js