Share
Embed
Head First Backbone.js
Sample code on GitHub: https://github.com/jaceju/Head-First-Backbone.js
大澤木小鐵
March 15, 2012
Transcript
100 slides
-
Separation of concerns Router Model View Url and Pages Data
and Status DOM and Template -
Load these scripts underscore.js jQuery Backbone.js Your JavaScript <script src="/lib/underscore.js"></script>
<script src="/lib/jquery.js"></script> <script src="/lib/backbone.js"></script> <script src="/js/your_app.js"></script> -
Load these scripts underscore.js jQuery Backbone.js Your JavaScript <script src="/lib/underscore.js"></script>
<script src="/lib/jquery.js"></script> <script src="/lib/backbone.js"></script> <script src="/js/your_app.js"></script> -
Load these scripts underscore.js jQuery Backbone.js Your JavaScript <script src="/lib/underscore.js"></script>
<script src="/lib/jquery.js"></script> <script src="/lib/backbone.js"></script> <script src="/js/your_app.js"></script> -
Load these scripts underscore.js jQuery Backbone.js Your JavaScript <script src="/lib/underscore.js"></script>
<script src="/lib/jquery.js"></script> <script src="/lib/backbone.js"></script> <script src="/js/your_app.js"></script> -
Basic flow Find out input UI and output UI, Define
model and business logic, Define view elements, Define events, Define router (optional), Initialize all objects and run. -
Basic flow Find out input UI and output UI, Define
model and business logic, Define view elements, Define events, Define router (optional), Initialize all objects and run. -
Basic flow Find out input UI and output UI, Define
model and business logic, Define view elements, Define events, Define router (optional), Initialize all objects and run. -
Basic flow Find out input UI and output UI, Define
model and business logic, Define view elements, Define events, Define router (optional), Initialize all objects and run. -
Basic flow Find out input UI and output UI, Define
model and business logic, Define view elements, Define events, Define router (optional), Initialize all objects and run. -
Basic flow Find out input UI and output UI, Define
model and business logic, Define view elements, Define events, Define router (optional), Initialize all objects and run. -
var target = {}; _.extend(target, Backbone.Events, { }); var init
= function () {}; target.on('init', init); target.on('start', function () {}); target.trigger('init'); target.trigger('init start'); Bind event and trigger may not be in the order -
var context = { command: 'this is context' }; var
execute = function () { console.log(this.command); }; target.on('run execute', execute, context); target.trigger('run execute'); Multi-events different events, same callback -
var context1 = { command: 'this is context1' }; var
context2 = { command: 'this is context2' }; var execute = function () { console.log(this.command); }; target.on('run execute', execute, context1); target.on('run execute', execute, context2); target.on('run execute', function () { console.log(this.command); }, context2); target.on('test', function () {}); target.off('run execute', execute, context1); target.off('run execute', execute); target.off('run execute'); target.off(); Unbind events -
var context1 = { command: 'this is context1' }; var
context2 = { command: 'this is context2' }; var execute = function () { console.log(this.command); }; target.on('run execute', execute, context1); target.on('run execute', execute, context2); target.on('run execute', function () { console.log(this.command); }, context2); target.on('test', function () {}); target.off('run execute', execute, context1); target.off('run execute', execute); target.off('run execute'); target.off(); Unbind events -
var context1 = { command: 'this is context1' }; var
context2 = { command: 'this is context2' }; var execute = function () { console.log(this.command); }; target.on('run execute', execute, context1); target.on('run execute', execute, context2); target.on('run execute', function () { console.log(this.command); }, context2); target.on('test', function () {}); target.off('run execute', execute, context1); target.off('run execute', execute); target.off('run execute'); target.off(); Unbind events -
var context1 = { command: 'this is context1' }; var
context2 = { command: 'this is context2' }; var execute = function () { console.log(this.command); }; target.on('run execute', execute, context1); target.on('run execute', execute, context2); target.on('run execute', function () { console.log(this.command); }, context2); target.on('test', function () {}); target.off('run execute', execute, context1); target.off('run execute', execute); target.off('run execute'); target.off(); Unbind events -
var context1 = { command: 'this is context1' }; var
context2 = { command: 'this is context2' }; var execute = function () { console.log(this.command); }; target.on('run execute', execute, context1); target.on('run execute', execute, context2); target.on('run execute', function () { console.log(this.command); }, context2); target.on('test', function () {}); target.off('run execute', execute, context1); target.off('run execute', execute); target.off('run execute'); target.off(); Unbind events -
How to share status ? <div class="input"> <input type="checkbox" id="switch"
value="on" /> <label for="switch">Turn on music.</label> </div> <div class="output"> Music: <span id="status" class="off">off</span> </div> -
var music = false; // after DOM is ready $('#switch').on('click',
function () { music = $(this).prop('checked'); var status = music ? 'on' : 'off'; $('#status') .removeClass('on off') .addClass(status) .text(status); }); Share status the old way global variable -
var music = false; // after DOM is ready $('#switch').on('click',
function () { music = $(this).prop('checked'); var status = music ? 'on' : 'off'; $('#status') .removeClass('on off') .addClass(status) .text(status); }); Share status the old way Input logic -
var music = false; // after DOM is ready $('#switch').on('click',
function () { music = $(this).prop('checked'); var status = music ? 'on' : 'off'; $('#status') .removeClass('on off') .addClass(status) .text(status); }); Share status the old way Output logic -
var Config = Backbone.Model.extend({ defaults: { music: false } });
var config = new Config(); $('#switch').on('click', function () { config.set('music', $(this).prop('checked')); }); config.on('change', function () { var music = config.get('music'); var status = music ? 'on' : 'off'; $('#status') .removeClass('on off') .addClass(status) .text(status); }); Share status the new way model -
var Config = Backbone.Model.extend({ defaults: { music: false } });
var config = new Config(); $('#switch').on('click', function () { config.set('music', $(this).prop('checked')); }); config.on('change', function () { var music = config.get('music'); var status = music ? 'on' : 'off'; $('#status') .removeClass('on off') .addClass(status) .text(status); }); Share status the new way trigger change -
var Config = Backbone.Model.extend({ defaults: { music: false } });
var config = new Config(); $('#switch').on('click', function () { config.set('music', $(this).prop('checked')); }); config.on('change', function () { var music = config.get('music'); var status = music ? 'on' : 'off'; $('#status') .removeClass('on off') .addClass(status) .text(status); }); output logic Share status the new way -
Change attributes var Box = Backbone.Model.extend({ defaults: { width: 100,
height: 100 } }); var box = new Box(); box.on("change", function () { ... }, box) .on("change:width", function () { ... }, box); box.set("width", 400); box.set("height", 300); define model -
Change attributes var Box = Backbone.Model.extend({ defaults: { width: 100,
height: 100 } }); var box = new Box(); box.on("change", function () { ... }, box) .on("change:width", function () { ... }, box); box.set("width", 400); box.set("height", 300); define events -
Change attributes var Box = Backbone.Model.extend({ defaults: { width: 100,
height: 100 } }); var box = new Box(); box.on("change", function () { ... }, box) .on("change:width", function () { ... }, box); box.set("width", 400); box.set("height", 300); trigger change and change:width -
Change attributes var Box = Backbone.Model.extend({ defaults: { width: 100,
height: 100 } }); var box = new Box(); box.on("change", function () { ... }, box) .on("change:width", function () { ... }, box); box.set("width", 400); box.set("height", 300); trigger change -
var Box = Backbone.Model.extend({ defaults: { width: 100, height: 100
}, validate: function(attrs) { if (attrs.width < 1) { return "Width cannot be less than 1"; } if (attrs.height < 1) { return "Height cannot be less than 1"; } } }); var box = new Box(); box.on("error", function(model, error) { alert(error); }); box.set('width', -1); Validate attributes add validate method -
var Box = Backbone.Model.extend({ defaults: { width: 100, height: 100
}, validate: function(attrs) { if (attrs.width < 1) { return "Width cannot be less than 1"; } if (attrs.height < 1) { return "Height cannot be less than 1"; } } }); var box = new Box(); box.on("error", function(model, error) { alert(error); }); box.set('width', -1); Validate attributes trigger error -
Use Collection var Note = Backbone.Model.extend({ defaults: { title: '',
content: '' }, urlRoot: '/note' }); var Notes = Backbone.Collection.extend({ model: Note }); var notes = new Notes([ { id: 1, title: 'note 1', content: 'This is note 1.' }, { id: 3, title: 'note 3', content: 'This is note 3.' }, { id: 5, title: 'note 5', content: 'This is note 5.' }, { id: 6, title: 'note 6', content: 'This is note 6.' }, { id: 9, title: 'note 9', content: 'This is note 9.' } ]); define model first -
Use Collection var Note = Backbone.Model.extend({ defaults: { title: '',
content: '' }, urlRoot: '/note' }); var Notes = Backbone.Collection.extend({ model: Note }); var notes = new Notes([ { id: 1, title: 'note 1', content: 'This is note 1.' }, { id: 3, title: 'note 3', content: 'This is note 3.' }, { id: 5, title: 'note 5', content: 'This is note 5.' }, { id: 6, title: 'note 6', content: 'This is note 6.' }, { id: 9, title: 'note 9', content: 'This is note 9.' } ]); define collection and set model class -
Use Collection var Note = Backbone.Model.extend({ defaults: { title: '',
content: '' }, urlRoot: '/note' }); var Notes = Backbone.Collection.extend({ model: Note }); var notes = new Notes([ { id: 1, title: 'note 1', content: 'This is note 1.' }, { id: 3, title: 'note 3', content: 'This is note 3.' }, { id: 5, title: 'note 5', content: 'This is note 5.' }, { id: 6, title: 'note 6', content: 'This is note 6.' }, { id: 9, title: 'note 9', content: 'This is note 9.' } ]); initialize data -
What inside ? note note note note note length =
5 0 1 2 3 4 index id 1 3 5 6 9 -
Get model by id note 0 1 2 3 4
get(id = 3) index id 1 3 5 6 9 var note = notes.get(3); note note note note -
note Get model by index note note note note at(index
= 3) 0 1 2 3 4 index id 1 3 5 6 9 var note = notes.at(3); -
note Remove model note note note note note remove 0
1 2 3 4 index id 1 3 5 6 9 var note = notes.at(2); notes.remove(note); // or notes.remove({ id: 5 }); length = 4 -
Add model note note note note note add length =
5 0 1 2 3 4 index id 1 3 6 9 10 var note = new Note({ id: 10, title: 'note 10', content: '...' }); notes.add(note); -
Create var note = new Note(); note.on('change', function () {
console.log('change: ' + this.id); console.log(note.url()); }); note.on('sync', function () { console.log('sync: ' + this.id); }); note.save({ title: 'note 10', content: 'This is content.' }); Backbone.sync trigger 'sync' trigger 'change' POST http://localhost/note note.url() → http://localhost/note/10 -
Backbone.sync trigger 'sync' trigger 'change' Create var note = new
Note(); note.on('change', function () { console.log('change: ' + this.id); console.log(note.url()); }); note.on('sync', function () { console.log('sync: ' + this.id); }); note.save({ title: 'note 10', content: 'This is content.' }, { wait: true }); waiting for sync completed -
Load var note = new Note({ id: 10 }); note.fetch({
success: function (model, response) {}, error: function (model, response) {} }); GET http://localhost/note/10 { "id":10, "title":"note 10", "content":"This is note 10." } trigger 'change' Backbone.sync -
Update note.save({ content: 'New Content Here.' }, { wait: true
}); PUT http://localhost/note/10 { "id":10, "title":"note 10", "content":"New Content Here." } Backbone.sync trigger 'sync' trigger 'change' -
Destroy note.on('destroy', function () {}); note.on('change', function () {}); note.on('sync',
function () {}); note.destroy(); DELETE http://localhost/note/10 Backbone.sync trigger 'sync' trigger 'destroy' won’t invoke -
Backbone.sync trigger 'sync' trigger 'add' Create by Collection var Notes
= Backbone.Collection.extend({ model: Note, url: '/note' }); var notes = new Notes(); notes.on('add', function () {}); notes.on('sync', function () {}); var note = notes.create({ 'title': 'note 11', 'content': 'This is note 11.' }, { wait: true }); POST http://localhost/note -
Load into Collection notes.on('add', function () {}); notes.fetch({ add: true
}); GET http://localhost/note Backbone.sync trigger 'add' trigger 'add' by length of collection ... -
Customize Backbone.sync Backbone.sync = function(method, model, options) { var response;
switch (method) { case "create": /* ... */ break; case "read": /* ... */ break; case "update": /* ... */ break; case "delete": /* ... */ break; } if (response) { options.success(model, response); } else { options.error(model, response); } }; Delegate to customize persistence strategy. Example: http://documentcloud.github.com/backbone/docs/backbone-localstorage.html -
What is Backbone.View ? Input & Output DOM events handling
Model binding Not Template -
What is Backbone.View ? Input & Output DOM events handling
Model binding Not Template -
What is Backbone.View ? Input & Output DOM events handling
Model binding Not Template -
What is Backbone.View ? Input & Output DOM events handling
Model binding Not Template -
Manage events var Switch = Backbone.View.extend({ events: { 'click #switch':
'toggleMusic' }, toggleMusic: function (e) { this.model.set('music', $(e.target).prop('checked')); } }); event -
Manage events var Switch = Backbone.View.extend({ events: { 'click #switch':
'toggleMusic' }, toggleMusic: function (e) { this.model.set('music', $(e.target).prop('checked')); } }); child element -
Manage events var Switch = Backbone.View.extend({ events: { 'click #switch':
'toggleMusic' }, toggleMusic: function (e) { this.model.set('music', $(e.target).prop('checked')); } }); callback -
Render var Status = Backbone.View.extend({ initialize: function () { this.model.on('change',
this.render, this); }, render: function () { var music = this.model.get('music'); var status = music ? 'on' : 'off'; $('#status', this.el) .removeClass('on off') .addClass(status) .text(status); } }); -
Render var Status = Backbone.View.extend({ initialize: function () { this.model.on('change',
this.render, this); }, render: function () { var music = this.model.get('music'); var status = music ? 'on' : 'off'; $('#status', this.el) .removeClass('on off') .addClass(status) .text(status); } }); -
Render var Status = Backbone.View.extend({ initialize: function () { this.model.on('change',
this.render, this); }, render: function () { var music = this.model.get('music'); var status = music ? 'on' : 'off'; $('#status', this.el) .removeClass('on off') .addClass(status) .text(status); } }); -
Put all together $(function () { var config = new
Config(); var switchView = new Switch({ el: '#input', model: config }); var statusView = new Status({ el: '#output', model: config }); }); after DOM ready -
Put all together $(function () { var config = new
Config(); var switchView = new Switch({ el: '#input', model: config }); var statusView = new Status({ el: '#output', model: config }); }); model -
Put all together $(function () { var config = new
Config(); var switchView = new Switch({ el: '#input', model: config }); var statusView = new Status({ el: '#output', model: config }); }); input -
Put all together $(function () { var config = new
Config(); var switchView = new Switch({ el: '#input', model: config }); var statusView = new Status({ el: '#output', model: config }); }); output -
switchView statusView config new Switch({ el: '#input', model: config })
new Status({ el: '#output', model: config }) new Config() -
events: { 'click #switch': 'toggleMusic' }, toggleMusic: function (e) {
... } switchView statusView config -
switchView statusView config initialize: function () { this.model.on( 'change', this.render,
this); }, render: function () { ... } -
Template <script type="text/template" id="note-template"> <article> <h1><%= title %></h1> <p><%= content
%></p> </article> </script> var NoteView = Backbone.View.extend({ tagName: 'li', template: _.template($('#note-template').html()), render: function () { this.$el.html(this.template(this.model.toJSON())); return this; } }); template -
Template <script type="text/template" id="note-template"> <article> <h1><%= title %></h1> <p><%= content
%></p> </article> </script> var NoteView = Backbone.View.extend({ tagName: 'li', template: _.template($('#note-template').html()), render: function () { this.$el.html(this.template(this.model.toJSON())); return this; } }); View <li> <article> <h1>note 5</h1> <p>This is note 5.</p> </article> </li> -
HTML Container <ul id="notes"> </ul> var NotesView = Backbone.View.extend({ initialize:
function () { this.render.apply(this); }, render: function () { this.collection.forEach(function (model) { var noteView = new NoteView({ model: model, }); this.$el.append(noteView.render().el); }, this); } }); HTML -
HTML Container <ul id="notes"> </ul> var NotesView = Backbone.View.extend({ initialize:
function () { this.render.apply(this); }, render: function () { this.collection.forEach(function (model) { var noteView = new NoteView({ model: model, }); this.$el.append(noteView.render().el); }, this); } }); View -
HTML Container <ul id="notes"> </ul> var NotesView = Backbone.View.extend({ initialize:
function () { this.render.apply(this); }, render: function () { this.collection.forEach(function (model) { var noteView = new NoteView({ model: model, }); this.$el.append(noteView.render().el); }, this); } }); render each note -
Template Engines Underscore.js template http://documentcloud.github.com/underscore/#template JavaScript Micro-Templating http://ejohn.org/blog/javascript-micro-templating/ jQuery Templates
Plugin https://github.com/jquery/jquery-tmpl EJS http://embeddedjs.com/ -
Single page app <article> ... <ul> <li><a href="#page/1">Page 1</a></li> <li><a
href="#page/2">Page 2</a></li> <li><a href="#page/3">Page 3</a></li> </ul> </article> <footer> <ul> <li><a href="#help">Help</a></li> <li><a href="#">Home</a></li> </ul> </footer> -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); not 'routers' !!! -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); http://localhost/ http://localhost/# -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); http://localhost/#page/1 http://localhost/#page/2 http://localhost/#page/3 -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); http://localhost/#help -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); other url hash (404) -
Single page app var App = {}; App.Router = Backbone.Router.extend({
routes: { '': 'index', 'page/:page': 'page', 'help': 'help', '*error': 'notFound' }, index: function () {}, page: function (page) {}, help: function () {}, notFound: function () {} }); put the models and views here -
Starting route $(function () { var router = new App.Router();
Backbone.history.start(); }); not 'Backbone.History' -
Push state $(function () { var router = new App.Router();
Backbone.history.start({ pushState: true }); }); Only HTML5 support, Use iframe in old IE relocate http://localhost/#page/1 ! http://localhost/#help ! http://localhost/page/1 http://localhost/help ! ! -
What happens ? $(function () { var router = new
App.Router(); Backbone.history.start({ pushState: true }); }); relocate http://localhost/#page/1 ! http://localhost/#help ! http://localhost/page/1 http://localhost/help ! ! Will not change url until reload -
What happens ? $(function () { var router = new
App.Router(); Backbone.history.start({ pushState: true }); }); relocate http://localhost/#page/1 ! http://localhost/#help ! http://localhost/page/1 http://localhost/help ! ! Should be render same result when you reload the page. -
What happens ? $(function () { var router = new
App.Router(); Backbone.history.start({ pushState: true }); }); relocate http://localhost/#page/1 ! http://localhost/#help ! http://localhost/page/1 http://localhost/help ! ! http://localhost/page/1#page/2 but ... trigger nothing -
Navigate url var usePushState = true; Backbone.history.start({ pushState: usePushState });
if (usePushState) { $('a[href^="#"]').on('click', function (e) { e.preventDefault(); router.navigate($(this).attr('href'), true); }); } don’t use prop method -
Pushstate in sub directory Backbone.history.start({ pushState: true, root: '/subdir/' });
use root when your app in sub directory Example: http://localhost/subdir/#page/2