understanding backbonejs

39
Client-side MVC with Backbone.js Jquery to Backbone.js

Upload: nick-lee

Post on 08-May-2015

2.045 views

Category:

Technology


3 download

DESCRIPTION

Understanding Backbone.js

TRANSCRIPT

Page 1: Understanding backbonejs

Client-side MVC with Backbone.js

Jquery to Backbone.js

Page 2: Understanding backbonejs

Agenda

• Why Backbone.js

• Architecture

• Jquery to Backbone.js

• Real World Examples

• Resources

• Extras

• Questions

Page 3: Understanding backbonejs

Why backbonejs

• From server-side to client-side

• From server-side to client-side We need efficient tools

• Jquery is cool but…

Page 4: Understanding backbonejs

Why backbone.js

• We have to store object informations into the DOM

var list = "";

$.each(data, function (index, value) {

list += "<li id="item-" + value.Id + "">" + value.Name + "</li>"; });

$("ul").append(list);

Page 5: Understanding backbonejs

Callback hell

jQuery callback hell

$.getJSON("/Items", function (data) { var list = ""; $.each(data, function (index, value) { list += "<li id="item-" + value.Id + "">" + value.Name + "</li>"; } ); $("ul").append(list); $("li").click(function () { var $this = $(this); var id = $this.attr("id").replace("item-", ""); $.post("/Items", { id: id }, function () { $this.fadeOut(function () { $this.remove(); }); }); });

Page 6: Understanding backbonejs

Why Backbone.js

“Its all too easy to create JavaScript applications that end up as tangled piles of Jquery selectors and callbacks.”

• So, what do we need?

• Abstraction.

• Decoupling UI from Data.

• No more callbacks.

Page 7: Understanding backbonejs

Why Backbone.js

• A RESTful service based data layer.

• Events (to keep UI and data up-to-date).

• A template engine.

• A solid routing system.

• All the above things wrapped into a lightweight JavaScript framework.

Page 8: Understanding backbonejs

Architecture

• Oct 13th, 2010 Jeremy Ashkenas

• http://documentcloud.github.com/backbone

• https://github.com/documentcloud/backbone

• @documentcloud

• #documentcloud on IRC

• https://groups.google.com/forum/#!forum/backbonejs

Page 9: Understanding backbonejs

Architecture

• Backbone.js gives structure to web applications by providing models with key-value binding and custom events,

• collections with a rich API of enumerable functions,

• views with declarative event handling,

• and connects it all to your existing API over a RESTful JSON interface.

Page 10: Understanding backbonejs

Dependencies

• jQuery or Zepto

• Underscore.js

• Json2.js

Page 11: Understanding backbonejs

MVC

• Model / Collection

• Template (View)

• View (Controller)

• Router

Page 12: Understanding backbonejs

Model

• Representing data (auto-generated).

• Handling persistence.

• Throws events.

• Reusable.

Page 13: Understanding backbonejs

Model

• Fetch HTTP GET /url

• Save (new) HTTP POST /url

• Save HTTP PUT /url/id

• Destroy HTTP DELETE /url/id

Page 14: Understanding backbonejs

Model

var Item = Backbone.Model.extend({

idAttribute: “Id”,

urlRoot: “/Items”

});

Page 15: Understanding backbonejs

Model

var item = new Item();

item.set({ Name: “Nick” }); // trigger change

item.save(); // trigger sync

Page 16: Understanding backbonejs

Model

http://backbonejs.org/#Model

Page 17: Understanding backbonejs

Collection

• A list of models.

• Underscore methods.

Page 18: Understanding backbonejs

Collection

var Items = Backbone.Collection.extend({ model: Item, url: "/Items“ }); var items = new Items(); items.fetch(); // trigger reset items.comparator = function(item) { return item.get("Name"); }

Page 19: Understanding backbonejs

Collection

http://backbonejs.org/#Collection

Page 20: Understanding backbonejs

View

• Manipulates the DOM.

• Delegates DOM events.

• Has a Model / Collection.

Page 21: Understanding backbonejs
Page 22: Understanding backbonejs

View

var ListView = Backbone.View.extend({

el: $("ul"),

initialize: function() {

this.collection.bind("reset", this.render, this);

},

render: function() {

this.collection.each(this.addItem, this);

return this;

},

Page 23: Understanding backbonejs

addItem: function(item) { var itemView = new ItemView({ var ItemView = Backbone.View.extend({ model: item tagName: "li", }); render: function() { this.$el.append(itemView.el); this.$el.text(this.model.get("Name")); itemView.render(); return this; } } }); });

Page 24: Understanding backbonejs

View

var items = new Items();

var listView = new ListView({

collection: items

});

items.fetch();

Page 25: Understanding backbonejs

View

http://backbonejs.org/#View

Page 26: Understanding backbonejs

Template(Underscore.js)

Compiles JavaScript templates into functions that can be evaluated for rendering.

• Mustache

• jQuery - tmpl

Page 27: Understanding backbonejs

Template

<script type = “text/template” id = “item-template” > <li><%= Name %></li> </script> var ItemView = Backbone.View.extend({ … template: _.template($("#item-template").html()), ... render: function() { this.$el.html(this.template(this.model.toJSON())); return this; } … });

Page 28: Understanding backbonejs

Router

• Maps urls to function. • Enable history / bookmarking. var AppRouter = Backbone.Router.extend({ routes: { "": "init" }, init: function() { … } });

Page 29: Understanding backbonejs

Router

window.AppRouter = Backbone.Router.extend({ routes: { "": "loadInvoices", "/add": "addInvoice", "/show/:id": "showInvoice", "/edit/:id": "editInvoice" }, loadInvoices: function() {… }, addInvoice: function() {… }, showInvoice: function(id) {… }, editInvoice: function(id) {… } });

Page 30: Understanding backbonejs

Router

http://backbonejs.org/#Router

Page 31: Understanding backbonejs

Architecture

Page 32: Understanding backbonejs

Jquery to Backbone.js

$(document).ready(function() { $('#new-status form').submit(function(e) { e.preventDefault(); $.ajax({ url: '/status', type: 'POST', dataType: 'json', data: { text: $('#new-status').find('textarea').val() }, success: function(data) { $('#statuses').append('<li>' + data.text + '</li>'); $('#new-status').find('textarea').val(''); } }); }); });

Page 33: Understanding backbonejs

Step to step from Jquery to Backbone.js

var Status = Backbone.Model.extend({

url: '/status'

});

var Statuses = Backbone.Collection.extend({

model: Status

});

var NewStatusView = Backbone.View.extend({

events: {

'submit form': 'addStatus'

},

initialize: function() {

this.collection.on('add', this.clearInput, this);

},

addStatus: function(e) {

e.preventDefault();

this.collection.create({ text: this.$('textarea').val() });

},

clearInput: function() {

this.$('textarea').val('');

}

});

Page 34: Understanding backbonejs

var StatusesView = Backbone.View.extend({ initialize: function() { this.collection.on('add', this.appendStatus, this); }, appendStatus: function(status) { this.$('ul').append('<li>' + status.escape('text') + '</li>'); } }); $(document).ready(function() { var statuses = new Statuses(); new NewStatusView({ el: $('#new-status'), collection: statuses }); new StatusesView({ el: $('#statuses'), collection: statuses }); });

Page 35: Understanding backbonejs

Real World

• DocumdentCloud

• LinkedIn mobile

• Pandora

• Airbnb

• Hulu

• BaseCamp

• Diaspora

http://backbonejs.org/#examples

Page 37: Understanding backbonejs

Extra

TDD – Jasmine http://pivotal.github.com/jasmine/ describe("Todo", function() { it("Should have a title", function() { var todo = new Todo(); expect(todo.get("title")).toBeDefined(); }); });

Page 38: Understanding backbonejs

Pros and Cons

+

• Lightweight

• Powerful

• Code is clean(and maintainable)

-

• Too verbose(for small applications)

Page 39: Understanding backbonejs

Questions?

Thanks