Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Backbone.js

Avatar for jcon jcon
April 04, 2012

Introduction to Backbone.js

A brief introduction to Backbone.js intended for people knowledgable about jQuery. We also walk through building a simple Backbone.js based twitter client. The full project source code is here: https://github.com/jcon/backbone-tweetflow

Avatar for jcon

jcon

April 04, 2012
Tweet

Other Decks in Technology

Transcript

  1. @newfoo 8IZ#BDLCPOF  Even this can get out of hand

    courtesy of http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/ Wednesday, April 4, 12
  2. @newfoo 8IZ#BDLCPOF  •Callbacks take effort to organize •Hard to

    maintain •Views & app logic intermingled Wednesday, April 4, 12
  3. @newfoo 8IZ/PU +4 •Ember (aka SproutCore 2.0) •ExtJS (or Sencha

    Touch) •Knockout JS Wednesday, April 4, 12
  4. @newfoo #BDLCPOF`T7JFX •Lightweight MVC •or Model View ViewController •Provides simple

    framework •Easy to learn (1400 SLOC) Wednesday, April 4, 12
  5. @newfoo .PEFMT tf.models.Tweet = Backbone.Model.extend({ defaults: { from_user: "tweetflow", from_user_name:

    "TweetFlow", text: "Test Tweet" } }); var tweet = new tf.models.Tweet(); tweet.set({“from_user”: “yoda”}); tweet.get(“from_user”); => “yoda” Wednesday, April 4, 12
  6. @newfoo 7JFXT •Make these with HTML •Can be in templates

    or right in HTML Wednesday, April 4, 12
  7. @newfoo 7JFXT <script id="tweet-tpl" type="text/template"> <img src="<%= profile_image_url %>" />

    <h2><%= from_user_name %></h2> <h3>@<%= from_user %></h3> <p><%= text %></p> </script> var tpl = _.template($(“#tweet-tpl”).html()); tpl({“text”: “A tweet”, ...}) => rendered template Wednesday, April 4, 12
  8. @newfoo #BDLCPOF7JFXT views.TweetItemView = Backbone.View.extend({ el: 'li', template: tpl('tweet-item-tpl'), render:

    function() { this.el = this.template(this.model.toJSON()); return this; } }); var tweet = new tf.models.Tweet({ ... }); var tweetItem = new views.TweetItemView({ model: tweet }); $(“#container”).append(tweetItem.render().el); Wednesday, April 4, 12
  9. @newfoo "#BDLCPOF"QQ 4USVDUVSF •jQuery / Zepto (for mobile) •Underscore •Backbone

    •Your own JS / Backbone Extensions Wednesday, April 4, 12