Understanding backbonejs
Upcoming SlideShare
Loading in...5
×
 

Understanding backbonejs

on

  • 1,977 views

Understanding Backbone.js

Understanding Backbone.js

Statistics

Views

Total Views
1,977
Views on SlideShare
1,977
Embed Views
0

Actions

Likes
1
Downloads
104
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Understanding backbonejs Understanding backbonejs Presentation Transcript

  • Client-side MVC with Backbone.js Jquery to Backbone.js
  • Agenda• Why Backbone.js• Architecture• Jquery to Backbone.js• Real World Examples• Resources• Extras• Questions
  • Why backbonejs• From server-side to client-side• From server-side to client-side We need efficient tools• Jquery is cool but…
  • 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);
  • Callback helljQuery 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(); }); }); });
  • 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.
  • 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.
  • 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/ba ckbonejs
  • 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.
  • Dependencies• jQuery or Zepto• Underscore.js• Json2.js
  • MVC• Model / Collection• Template (View)• View (Controller)• Router
  • Model• Representing data (auto-generated).• Handling persistence.• Throws events.• Reusable.
  • Model• Fetch HTTP GET /url• Save (new) HTTP POST /url• Save HTTP PUT /url/id• Destroy HTTP DELETE /url/id
  • Modelvar Item = Backbone.Model.extend({ idAttribute: “Id”, urlRoot: “/Items”});
  • Modelvar item = new Item();item.set({ Name: “Nick” }); // trigger changeitem.save(); // trigger sync
  • Modelhttp://backbonejs.org/#Model
  • Collection• A list of models.• Underscore methods.
  • Collectionvar Items = Backbone.Collection.extend({ model: Item, url: "/Items“});var items = new Items();items.fetch(); // trigger resetitems.comparator = function(item) { return item.get("Name");}
  • Collectionhttp://backbonejs.org/#Collection
  • View• Manipulates the DOM.• Delegates DOM events.• Has a Model / Collection.
  • Viewvar 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; },
  • 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; } } });});
  • Viewvar items = new Items();var listView = new ListView({ collection: items});items.fetch();
  • Viewhttp://backbonejs.org/#View
  • Template(Underscore.js)Compiles JavaScript templates into functions that can be evaluated for rendering.• Mustache• jQuery - tmpl
  • 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; } …});
  • Router• Maps urls to function.• Enable history / bookmarking.var AppRouter = Backbone.Router.extend({ routes: { "": "init" }, init: function() { … }});
  • Routerwindow.AppRouter = Backbone.Router.extend({ routes: { "": "loadInvoices", "/add": "addInvoice", "/show/:id": "showInvoice", "/edit/:id": "editInvoice" }, loadInvoices: function() {… }, addInvoice: function() {… }, showInvoice: function(id) {… }, editInvoice: function(id) {… }});
  • Routerhttp://backbonejs.org/#Router
  • Architecture
  • 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(); } }); });});
  • Step to step from Jquery tovar Status = Backbone.Model.extend({ Backbone.js 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(); }});
  • 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 });});
  • Real World• DocumdentCloud• LinkedIn mobile• Pandora• Airbnb• Hulu• BaseCamp• Diasporahttp://backbonejs.org/#examples
  • Resource• https://github.com/documentcloud/backbone /wiki• https://github.com/documentcloud/backbone /wiki/Extensions%2C-Plugins%2C-Resources• http://backbonetutorials.com/• http://addyosmani.github.com/backbone- fundamentals/
  • ExtraTDD – Jasminehttp://pivotal.github.com/jasmine/describe("Todo", function() { it("Should have a title", function() { var todo = new Todo(); expect(todo.get("title")).toBeDefined(); });});
  • Pros and Cons+• Lightweight• Powerful• Code is clean(and maintainable)-• Too verbose(for small applications)
  • Questions? Thanks