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

An introduction to Backbone.js

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

An introduction to Backbone.js

JavaScript libraries and frameworks keep coming out. It seems the community is addressing problems we all have in common with a wide range of solutions.

In these slides we analyze what these problems are, why we would need a JavaScript framework and how to pick one.

Lastly we are gonna go through Backbone classes and a really basic example of them in action.

Avatar for Vero Rebagliatte

Vero Rebagliatte

March 22, 2013
Tweet

More Decks by Vero Rebagliatte

Other Decks in Programming

Transcript

  1. Forecast • What’s backbone.js • Why to use a library/framework

    • Alternatives • How to pick one (libraries vs frameworks) • Backbone classes • Backbone examples
  2. Alternatives • Spine • Knockout • Angular • JavaScript MVC

    • Capuccino • SproutCore • Batman.js • Coherent • Ember • Ext • Meteor • Agility • Google Closure • Google Web Toolkit
  3. window.Twitter = Models: {} Collections: {} Views: {} Routers: {}

    initialize: -> new Twitter.Routers.Tweets() Backbone.history.start() $(document).ready -> Twitter.initialize() Initializing the app
  4. class Twitter.Routers.Tweets extends Backbone.Router routes: '': 'index' 'tweets': 'index' initialize:

    -> @collection = new Twitter.Collections.Tweets() @collection.fetch() index: -> view = new Twitter.Views.TweetsIndex(collection: @collection) $('#container').html(view.render().el) Router
  5. class Twitter.Routers.Tweets extends Backbone.Router routes: '': 'index' 'tweets': 'index' initialize:

    -> @collection = new Twitter.Collections.Tweets() @collection.fetch() index: -> view = new Twitter.Views.TweetsIndex(collection: @collection) $('#container').html(view.render().el) Router
  6. class Twitter.Routers.Tweets extends Backbone.Router routes: '': 'index' 'tweets': 'index' initialize:

    -> @collection = new Twitter.Collections.Tweets() @collection.fetch() index: -> view = new Twitter.Views.TweetsIndex(collection: @collection) $('#container').html(view.render().el) Router
  7. class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet'

    initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View
  8. class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet'

    initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View
  9. class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet'

    initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View
  10. class Twitter.Views.TweetsIndex extends Backbone.View template: JST['tweets/index'] events: 'submit #new_tweet': 'createTweet'

    initialize: -> @collection.on('add', @render, this) @collection.on('reset', @render, this) render: -> $(@el).html(@template(tweets: @collection)) this createTweet: (event) -> event.preventDefault() @collection.create name: $('#new_tweet_msg').val() View
  11. <h1>My Twitter App </h1> <form id="new_tweet"> <input type="text" name="msg" id="new_tweet_msg">

    <input type="submit" value="Tweet"> </form> <ul> <% for tweet in @.tweets.models: %> <li><%= tweet.get('content') %></li> <% end %> </ul> Template
  12. Recap • Instantiate router • Router: Map routes with functions,

    load collections on them • Collection: Associate a model and a url to touch the API • Model: Add attributes logic • View: Associate a template, pass in the collection and render the result, bind events.