SlideShare a Scribd company logo
Put some Backbone.js in
                         your apps




Wednesday, March 13, 13
Sid Maestre
                          Platform Evangelist
                              @SidneyAllen


Wednesday, March 13, 13
Agenda
                   •What, why and who
                   •Models & Collections
                   •Views & Templates
                   •Routers
                   •Events
                   •Data Persistence with StackMob
                   •Bonus! Require.js
Wednesday, March 13, 13
What




Wednesday, March 13, 13
Why

                   •Single Page Apps
                   •Structure
                   •Reuse
                   •Separation of roles of concerns

Wednesday, March 13, 13
Who




Wednesday, March 13, 13
MV* Landscape




Wednesday, March 13, 13
TodoMVC




           http://addyosmani.github.com/todomvc/

Wednesday, March 13, 13
My Wine App




Wednesday, March 13, 13
MVC
                                  Controller




                          Model                View



Wednesday, March 13, 13
MVC
                                  Controller




                          Model                View



Wednesday, March 13, 13
MVC
                                  Controller




                          Model                View



Wednesday, March 13, 13
MVC
                                  Controller




                          Model                View



Wednesday, March 13, 13
adding backbone
            src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
            src="http://static.stackmob.com/js/json2-min.js"></script>
            src="http://static.stackmob.com/js/underscore-1.4.1-min.js"></script>
            src="http://static.stackmob.com/js/backbone-0.9.2-min.js"></script>




Wednesday, March 13, 13
Model




Wednesday, March 13, 13
define

                   Wine = Backbone.Model.extend();

                   firstWine = new Wine({
                       name: 'Clos Pegase'
                   });




                                             DEMO TIME!



Wednesday, March 13, 13
Collections




Wednesday, March 13, 13
Define
                    Wine = Backbone.Model.extend();

                    Wines = Backbone.Collection.extend({
                      Model: Wine,
                      url: "#"
                    });




Wednesday, March 13, 13
Initialize
                   Wine = Backbone.Model.extend();

                   Wines = Backbone.Collection.extend({
                     Model: Wine,
                     url: "#"
                   });

                   wines = new Wines([
                     {name: "Robert Mondovi"},
                     { name: "CakeBread"}
                   ]);

                   console.log(wines.toJSON());




Wednesday, March 13, 13
Loop
                  Wine = Backbone.Model.extend();

                  Wines = Backbone.Collection.extend({
                    Model: Wine,
                    url: "#"
                  });

                  wines = new Wines([
                    {name: "Robert Mondovi"},
                    { name: "CakeBread"}
                  ]);


                  wines.each( function(model) {
                      console.log(model.get('name'));
                  });




                                            DEMO TIME!
Wednesday, March 13, 13
View




Wednesday, March 13, 13
define

                          HomeView = Backbone.View.extend({

                          });




                                               DEMO TIME!
Wednesday, March 13, 13
render

                          HomeView = Backbone.View.extend({

                            render: function() {
                              this.$el.append("<h1>My App</h1>");

                                return this;
                            }

                          });




Wednesday, March 13, 13
initialize
                          HomeView = Backbone.View.extend({

                            initialize: function() {
                               this.render();
                            },

                            render: function() {
                              this.$el.append("<h1>My App</h1>");

                                return this;
                            }

                          });




                                                DEMO TIME!
Wednesday, March 13, 13
el
                          HomeView = Backbone.View.extend({

                                 el: 'body',

                                 initialize: function() {
                                    this.render();
                                 },

                                 render: function() {

                                   this.$el.empty();

                                   this.$el.append("<h1>My App</h1>");

                                   return this;
                             }
                          });




                                                  DEMO TIME!
Wednesday, March 13, 13
list view
                   ListView = Backbone.View.extend({

                      tagName: 'ul',

                      initialize: function() {

                      },

                      render: function() {

                           this.$el.empty();
                           this.$el.append("<li>Hello</li>");
                           this.$el.append("<li>Goodbye</li>");

                           return this;
                      }

                   });




Wednesday, March 13, 13
append
                   HomeView = Backbone.View.extend({

                          el: 'body',

                          initialize: function() {
                             this.render();
                          },

                          render: function() {

                            this.$el.empty();
                            this.$el.append("<h1>My App</h1>");

                            this.listView = new ListView();
                            this.$el.append(this.listView.render().el);


                            return this;
                   }



                                                 DEMO TIME!
Wednesday, March 13, 13
Templates




Wednesday, March 13, 13
render
                  <script type="text/template" id="item-container">
                      <li><%= value %></li>
                  </script>



                  ListView = Backbone.View.extend({

                      tagName: 'ul',

                      initialize: function() {
                         this.template = _.template($('#item-container').html());
                      },

                      render: function() {
                          ...
                          this.$el.append(this.template({value : "Hello Backbone"}));

                              return this;
                          }
                  });




Wednesday, March 13, 13
looping
                  ListView = Backbone.View.extend({

                      ...

                      render: function() {
                        var el = this.$el,
                             template = this.template;

                          el.empty();

                          wines.each(function(wine){
                            el.append(template( wine.toJSON() ));
                          });

                          return this;
                      }

                  });



                                               DEMO TIME!
Wednesday, March 13, 13
Routes




Wednesday, March 13, 13
start
                   $(document).ready(function () {
                       wineApp = new AppRouter();
                       Backbone.history.start();
                   });




Wednesday, March 13, 13
routes
                  AppRouter = Backbone.Router.extend({

                     routes:{
                        "":"home",
                        "add":"add",
                        "close":"home",
                     },

                     home:function () {
                        console.log('home');
                        new HomeView();
                     },

                     add:function () {
                       console.log('add');
                       new AddView();
                     }

                  });




Wednesday, March 13, 13
methods
                  AppRouter = Backbone.Router.extend({

                     routes:{
                        "":"home",
                        "add":"add",
                        "cancel":"home",
                     },

                     home:function () {
                        console.log('home');
                        new HomeView();
                     },

                     add:function () {
                       console.log('add');
                       new AddView();
                     }

                  });




Wednesday, March 13, 13
fragments




                           DEMO TIME!
Wednesday, March 13, 13
events
                  AddView = Backbone.View.extend({

                     className:"span8",
                     tagName: "div",

                     events: {
                       "click #addBtn": "add",
                       ...
                      },

                          ...

                     add: function(e) {
                       e.preventDefault();

                           console.log($('#name').val());

                           return this;
                     }

                  });




Wednesday, March 13, 13
model / collection

                  Wine = Backbone.Model.extend();

                  Wines = Backbone.Collection.extend({
                    Model: Wine,
                    url: "#"
                  });

                  wines = new Wines();




Wednesday, March 13, 13
add to collection

                    add: function(e) {
                      e.preventDefault();

                          var newWine = new Wine({name:$('#name').val() });

                          wines.add(newWine);

                          $('#name').val('');

                          return this;
                    }




Wednesday, March 13, 13
render listView
                   HomeView = Backbone.View.extend({

                   ...

                      render: function() {

                          var el = this.$el

                          el.empty();
                          el.append(this.template());

                          var listView = new ListView({collection:wines});
                          $('.span4').append(listView.render().el);

                          return this;
                      }

                   });




Wednesday, March 13, 13
ListView
                   ListView = Backbone.View.extend({

                      ...
                      initialize: function() {
                         this.collection.bind('all', this.render,this);
                         this.template = _.template($('#item-list').html());
                      },

                      render:function (eventName) {
                        var template = this.template,
                                  el = this.$el,
                          collection = this.collection;

                          $(".span4 ul").empty();

                          collection.each(function(wine){
                            el.append(template(wine.toJSON()));
                          });

                     ...
                     }
                   });




Wednesday, March 13, 13
event binding




                             DEMO TIME!
Wednesday, March 13, 13
free course

                                   Text




                          bit.ly/freebackbonejs

Wednesday, March 13, 13
Wednesday, March 13, 13
Easy to use SDKs




Wednesday, March 13, 13
Easy to use SDKs




Wednesday, March 13, 13
Wednesday, March 13, 13
010101
                          010101
                          010101
                          010101
                          010101
                          010101




Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
Wednesday, March 13, 13
StackMob is REST
                     // REST
                     http://api.stackmob.com/user
                                               ../user/1234
                                               ../chatmessage
                                               ../yourcustommethod




Wednesday, March 13, 13
Let’s create an app




Wednesday, March 13, 13
StackMob JS

              src="/libs/jquery/1.8.2/jquery.min.js"></script>
              src="/js/stackmob-js-0.8.0-bundled-min.js"></script>




Wednesday, March 13, 13
Add Init

                    StackMob.init({
                       appName: "a_wineapp",
                        clientSubdomain: "stackmob339",
                        publicKey: "d235e941-2139-40e6-9d2f-5b50b705198c",
                        apiVersion: 0
                    });




Wednesday, March 13, 13
model / collection
                     var Wine = StackMob.Model.extend({
                       schemaName: "wines"
                     });

                     var Wines = StackMob.Collection.extend({
                       model: Wine
                     });

                     var wines = new Wines();
                     wines.fetch({async: true});




Wednesday, March 13, 13
model.create
                    save: function(e) {
                      var collection = this.collection;
                      e.preventDefault();

                          var wine = new Wine({name:$('#name').val() });

                          wine.create({
                            success: function(model){
                              collection.add(model);
                            }
                          });

                          $('#name').val('');

                          return this;
                    }




Wednesday, March 13, 13
load model
                    <script type="text/template" id="item-list">
                      <li><a href="#update/<%= wines_id %>"><%= name %></a></li>
                    </script>




                     AppRouter = Backbone.Router.extend({

                          routes:{
                            "":"home",
                            "add":"add",
                            "update/:id":"update"
                           },

                       update:function(e) {
                         model = wines.get(e);
                         new UpdateView({model: model});
                       }
                     });




Wednesday, March 13, 13
UpdateView
                  var UpdateView = Backbone.View.extend({
                     ...
                     initialize: function() {
                         this.template = _.template($('#item-edit').html());
                         this.model = this.options.model;
                         this.render();
                      },

                              render: function() {
                                 $('.span8').remove();
                                 $(this.el).html(this.template(this.model.toJSON()));
                                 $('.row').append(this.el);
                                 return this;
                              },

                              save: function(e) {
                                 e.preventDefault();
                                 this.model.save({name:$('#name').val()},{
                                 success: function(model) {

                                 }
                               });
                               return this;
                          }
                  });


Wednesday, March 13, 13
HTML5 Hosting

                   •Github Integration
                   •We host your app
                   •API is Free
                   •StackMob Marketplace

Wednesday, March 13, 13
Wednesday, March 13, 13
Backbone & Angular

                          bit.ly/bbangular

               https://github.com/ericterpstra/ngSignIt




Wednesday, March 13, 13
Developing Backbone.js Applications




Wednesday, March 13, 13
free course

                                   Text




                          bit.ly/freebackbonejs

Wednesday, March 13, 13
Sid Maestre

                             Contact
                           sid@stackmob.com
                              @SidneyAllen




Wednesday, March 13, 13

More Related Content

PDF
How and why you should test
PDF
JavaScript Hacks for Hipsters
PDF
EMERGING_VISIONS_KEY_FINDINGS
PDF
الجين فى الفضاء الميكرسكوبى للخليه
PPT
Eval task 1 olivia h
PDF
Build your own heroku with cloud foundry
PDF
Backbone
PDF
Pragmatic JavaScript
How and why you should test
JavaScript Hacks for Hipsters
EMERGING_VISIONS_KEY_FINDINGS
الجين فى الفضاء الميكرسكوبى للخليه
Eval task 1 olivia h
Build your own heroku with cloud foundry
Backbone
Pragmatic JavaScript

Similar to Intro tobackbone (20)

PDF
Rails Intro & Tutorial
PDF
Ember and containers
PDF
Stop Ember Time
PDF
Keeping it small - Getting to know the Slim PHP micro framework
PDF
I motion
PDF
Symfony2 and MongoDB - MidwestPHP 2013
PDF
PDF
Intro to Ember.js
PDF
Complex Architectures in Ember
PDF
batou - multi(component|host|environment|.*) deployment
PDF
Silex: From nothing to an API
PDF
Standing on the shoulders of giants with JRuby
PDF
Backbone
PDF
spring3.2 java config Servler3
PDF
Introduction to JavaScript
PDF
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
PDF
Empezando con Twig
PDF
Building a Startup Stack with AngularJS
PDF
Puppet and AWS: Getting the best of both worlds
PDF
MongoTalk/Voyage
Rails Intro & Tutorial
Ember and containers
Stop Ember Time
Keeping it small - Getting to know the Slim PHP micro framework
I motion
Symfony2 and MongoDB - MidwestPHP 2013
Intro to Ember.js
Complex Architectures in Ember
batou - multi(component|host|environment|.*) deployment
Silex: From nothing to an API
Standing on the shoulders of giants with JRuby
Backbone
spring3.2 java config Servler3
Introduction to JavaScript
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
Empezando con Twig
Building a Startup Stack with AngularJS
Puppet and AWS: Getting the best of both worlds
MongoTalk/Voyage
Ad

Intro tobackbone

  • 1. Put some Backbone.js in your apps Wednesday, March 13, 13
  • 2. Sid Maestre Platform Evangelist @SidneyAllen Wednesday, March 13, 13
  • 3. Agenda •What, why and who •Models & Collections •Views & Templates •Routers •Events •Data Persistence with StackMob •Bonus! Require.js Wednesday, March 13, 13
  • 5. Why •Single Page Apps •Structure •Reuse •Separation of roles of concerns Wednesday, March 13, 13
  • 8. TodoMVC http://addyosmani.github.com/todomvc/ Wednesday, March 13, 13
  • 9. My Wine App Wednesday, March 13, 13
  • 10. MVC Controller Model View Wednesday, March 13, 13
  • 11. MVC Controller Model View Wednesday, March 13, 13
  • 12. MVC Controller Model View Wednesday, March 13, 13
  • 13. MVC Controller Model View Wednesday, March 13, 13
  • 14. adding backbone src="http://code.jquery.com/jquery-1.8.2.min.js"></script> src="http://static.stackmob.com/js/json2-min.js"></script> src="http://static.stackmob.com/js/underscore-1.4.1-min.js"></script> src="http://static.stackmob.com/js/backbone-0.9.2-min.js"></script> Wednesday, March 13, 13
  • 16. define Wine = Backbone.Model.extend(); firstWine = new Wine({ name: 'Clos Pegase' }); DEMO TIME! Wednesday, March 13, 13
  • 18. Define Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ Model: Wine, url: "#" }); Wednesday, March 13, 13
  • 19. Initialize Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ Model: Wine, url: "#" }); wines = new Wines([ {name: "Robert Mondovi"}, { name: "CakeBread"} ]); console.log(wines.toJSON()); Wednesday, March 13, 13
  • 20. Loop Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ Model: Wine, url: "#" }); wines = new Wines([ {name: "Robert Mondovi"}, { name: "CakeBread"} ]); wines.each( function(model) { console.log(model.get('name')); }); DEMO TIME! Wednesday, March 13, 13
  • 22. define HomeView = Backbone.View.extend({ }); DEMO TIME! Wednesday, March 13, 13
  • 23. render HomeView = Backbone.View.extend({ render: function() { this.$el.append("<h1>My App</h1>"); return this; } }); Wednesday, March 13, 13
  • 24. initialize HomeView = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { this.$el.append("<h1>My App</h1>"); return this; } }); DEMO TIME! Wednesday, March 13, 13
  • 25. el HomeView = Backbone.View.extend({ el: 'body', initialize: function() { this.render(); }, render: function() { this.$el.empty(); this.$el.append("<h1>My App</h1>"); return this; } }); DEMO TIME! Wednesday, March 13, 13
  • 26. list view ListView = Backbone.View.extend({ tagName: 'ul', initialize: function() { }, render: function() { this.$el.empty(); this.$el.append("<li>Hello</li>"); this.$el.append("<li>Goodbye</li>"); return this; } }); Wednesday, March 13, 13
  • 27. append HomeView = Backbone.View.extend({ el: 'body', initialize: function() { this.render(); }, render: function() { this.$el.empty(); this.$el.append("<h1>My App</h1>"); this.listView = new ListView(); this.$el.append(this.listView.render().el); return this; } DEMO TIME! Wednesday, March 13, 13
  • 29. render <script type="text/template" id="item-container"> <li><%= value %></li> </script> ListView = Backbone.View.extend({ tagName: 'ul', initialize: function() { this.template = _.template($('#item-container').html()); }, render: function() { ... this.$el.append(this.template({value : "Hello Backbone"})); return this; } }); Wednesday, March 13, 13
  • 30. looping ListView = Backbone.View.extend({ ... render: function() { var el = this.$el, template = this.template; el.empty(); wines.each(function(wine){ el.append(template( wine.toJSON() )); }); return this; } }); DEMO TIME! Wednesday, March 13, 13
  • 32. start $(document).ready(function () { wineApp = new AppRouter(); Backbone.history.start(); }); Wednesday, March 13, 13
  • 33. routes AppRouter = Backbone.Router.extend({ routes:{ "":"home", "add":"add", "close":"home", }, home:function () { console.log('home'); new HomeView(); }, add:function () { console.log('add'); new AddView(); } }); Wednesday, March 13, 13
  • 34. methods AppRouter = Backbone.Router.extend({ routes:{ "":"home", "add":"add", "cancel":"home", }, home:function () { console.log('home'); new HomeView(); }, add:function () { console.log('add'); new AddView(); } }); Wednesday, March 13, 13
  • 35. fragments DEMO TIME! Wednesday, March 13, 13
  • 36. events AddView = Backbone.View.extend({ className:"span8", tagName: "div", events: { "click #addBtn": "add", ... }, ... add: function(e) { e.preventDefault(); console.log($('#name').val()); return this; } }); Wednesday, March 13, 13
  • 37. model / collection Wine = Backbone.Model.extend(); Wines = Backbone.Collection.extend({ Model: Wine, url: "#" }); wines = new Wines(); Wednesday, March 13, 13
  • 38. add to collection add: function(e) { e.preventDefault(); var newWine = new Wine({name:$('#name').val() }); wines.add(newWine); $('#name').val(''); return this; } Wednesday, March 13, 13
  • 39. render listView HomeView = Backbone.View.extend({ ... render: function() { var el = this.$el el.empty(); el.append(this.template()); var listView = new ListView({collection:wines}); $('.span4').append(listView.render().el); return this; } }); Wednesday, March 13, 13
  • 40. ListView ListView = Backbone.View.extend({ ... initialize: function() { this.collection.bind('all', this.render,this); this.template = _.template($('#item-list').html()); }, render:function (eventName) { var template = this.template, el = this.$el, collection = this.collection; $(".span4 ul").empty(); collection.each(function(wine){ el.append(template(wine.toJSON())); }); ... } }); Wednesday, March 13, 13
  • 41. event binding DEMO TIME! Wednesday, March 13, 13
  • 42. free course Text bit.ly/freebackbonejs Wednesday, March 13, 13
  • 44. Easy to use SDKs Wednesday, March 13, 13
  • 45. Easy to use SDKs Wednesday, March 13, 13
  • 47. 010101 010101 010101 010101 010101 010101 Wednesday, March 13, 13
  • 58. StackMob is REST // REST http://api.stackmob.com/user                        ../user/1234                        ../chatmessage                        ../yourcustommethod Wednesday, March 13, 13
  • 59. Let’s create an app Wednesday, March 13, 13
  • 60. StackMob JS src="/libs/jquery/1.8.2/jquery.min.js"></script> src="/js/stackmob-js-0.8.0-bundled-min.js"></script> Wednesday, March 13, 13
  • 61. Add Init StackMob.init({    appName: "a_wineapp", clientSubdomain: "stackmob339", publicKey: "d235e941-2139-40e6-9d2f-5b50b705198c", apiVersion: 0 }); Wednesday, March 13, 13
  • 62. model / collection var Wine = StackMob.Model.extend({ schemaName: "wines" }); var Wines = StackMob.Collection.extend({ model: Wine }); var wines = new Wines(); wines.fetch({async: true}); Wednesday, March 13, 13
  • 63. model.create save: function(e) { var collection = this.collection; e.preventDefault(); var wine = new Wine({name:$('#name').val() }); wine.create({ success: function(model){ collection.add(model); } }); $('#name').val(''); return this; } Wednesday, March 13, 13
  • 64. load model <script type="text/template" id="item-list"> <li><a href="#update/<%= wines_id %>"><%= name %></a></li> </script> AppRouter = Backbone.Router.extend({ routes:{ "":"home", "add":"add", "update/:id":"update" }, update:function(e) { model = wines.get(e); new UpdateView({model: model}); } }); Wednesday, March 13, 13
  • 65. UpdateView var UpdateView = Backbone.View.extend({ ... initialize: function() { this.template = _.template($('#item-edit').html()); this.model = this.options.model; this.render(); }, render: function() { $('.span8').remove(); $(this.el).html(this.template(this.model.toJSON())); $('.row').append(this.el); return this; }, save: function(e) { e.preventDefault(); this.model.save({name:$('#name').val()},{ success: function(model) { } }); return this; } }); Wednesday, March 13, 13
  • 66. HTML5 Hosting •Github Integration •We host your app •API is Free •StackMob Marketplace Wednesday, March 13, 13
  • 68. Backbone & Angular bit.ly/bbangular https://github.com/ericterpstra/ngSignIt Wednesday, March 13, 13
  • 70. free course Text bit.ly/freebackbonejs Wednesday, March 13, 13
  • 71. Sid Maestre Contact sid@stackmob.com @SidneyAllen Wednesday, March 13, 13