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

Backbone.js

Avatar for Javier Arce Javier Arce
September 26, 2011

 Backbone.js

Avatar for Javier Arce

Javier Arce

September 26, 2011
Tweet

Other Decks in Programming

Transcript

  1. "Backbone supplies structure to JavaScript-heavy 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 application over a RESTful JSON interface."
  2. "Backbone supplies structure to JavaScript-heavy 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 application over a RESTful JSON interface."
  3. "Backbone supplies structure to JavaScript-heavy 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 application over a RESTful JSON interface."
  4. "Backbone supplies structure to JavaScript-heavy 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 application over a RESTful JSON interface."
  5. "Backbone supplies structure to JavaScript-heavy 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 application over a RESTful JSON interface."
  6. fs.stat(baseFolder, function(err, stats) { err && exit(err, null) || (function()

    { !stats.isDirectory() && exit(errors.NOTAREPO(baseFolder)) || (function() { fs.stat(objectsFolder, function(err, stats) { err && exit(err, null) || (function() { !stats.isDirectory() && exit(errors.NOTAREPO(objectsFolder)) || (function() { fs.readFile(indexFile, function(err, indexData) { err && exit(err, null) || (function() { fs.readFile(headFile, function(err, headData) { err && exit(err, null) || (function() { var odbObject = odb.ODB.init(); exit(null, new Repository(odbObject, indexData, headData, { 'repository':baseFolder, 'index':indexFile, 'head':headFile, 'objects':objectsFolder, 'bare':isBare })); })(); }); })(); }); })(); })(); }); })(); })(); }); Ventajas
  7. "With Backbone, you represent your data as models, which can

    be created, validated, destroyed, and saved to the server." Funcionamiento
  8. "Whenever a UI action causes an attribute of a model

    to change, the model triggers a "change" event; all the views that display the model's data are notified of the event, causing them to re-render." Funcionamiento
  9. "Whenever a UI action causes an attribute of a model

    to change, the model triggers a "change" event; all the views that display the model's data are notified of the event, causing them to re-render." Funcionamiento
  10. "You don't have to write the glue code that looks

    into the DOM to find an element with a specific id, and update the HTML manually — when the model changes, the views simply update themselves." Funcionamiento
  11. "You don't have to write the glue code that looks

    into the DOM to find an element with a specific id, and update the HTML manually — when the model changes, the views simply update themselves." Funcionamiento
  12. var Book = Backbone.Model.extend({…}); var book = new Book({ title:

    "Infinite Jest", author: "David F. Wallace" }); // "Nice book!" Models
  13. // Getters book.get("title"); // "Infinite Jest" book.get("author"); // "David F.

    Wallace" // Setters book.set({ title: "Infinite Jest", author: "David F. Wallace" }); Models
  14. // Eventos var Book = Backbone.Model.extend({ initialize: function(){ this.bind("change:title", function(){

    var title = this.get("title"); alert("Title changed to:" + title); }); } }); Models
  15. // Eventos var Book = Backbone.Model.extend({ initialize: function(){ this.bind("change:title", function(){

    var title = this.get("title"); alert("Title changed to:" + title); }); } }); book.set({ title: "Slaughterhouse-Five", author: "Kurt Vonnegut" }); // “Title changed to Slaughterhouse-Five” Models
  16. // Validación var Book = Backbone.Model.extend({ validate: function(attrs) { if

    (attrs.author == "Paulo Coelho") { return "Argh!"; } } }); var book = new Book({}); book.bind("error", function(model, error) { alert(error); }); book.set({ author: "Paulo Coelho" }); // “Argh!” Models
  17. Collections var BookCollection = Backbone.Collection.extend({ model: Book, url: "/books" });

    var Library = new BookCollection; var book = new Book({ title: "The Information", author: "James Gleick" });
  18. Collections var BookCollection = Backbone.Collection.extend({ model: Book, url: "/books" });

    var Library = new BookCollection; var book = new Book({ title: "The Information", author: "James Gleick" }); Library.add(book);
  19. Collections var BookCollection = Backbone.Collection.extend({ model: Book, url: "/books" });

    var Library = new BookCollection; var book = new Book({ title: "The Information", author: "James Gleick" }); Library.add(book); book.save();
  20. Collections var BookCollection = Backbone.Collection.extend({ model: Book, url: "/books" });

    var Library = new BookCollection; var book = new Book({ title: "The Information", author: "James Gleick" }); Library.add(book); book.save(); POST
  21. Collections var BookCollection = Backbone.Collection.extend({ model: Book, url: "/books" });

    var Library = new BookCollection; var book = new Book({ title: "The Information", author: "James Gleick" }); Library.add(book); book.save(); POST PUT
  22. Collections REST create → POST → /collection read → GET

    → /collection[/:id] update → PUT → /collection/:id delete → DELETE → /collection/:id
  23. forEach map reduce reduceRight find filter reject every some include

    invoke max chain min sortBy sortedIndex toArray size first rest last without indexOf lastIndexOf isEmpty Underscore.js
  24. var AppController = Backbone.Controller.extend({ routes: { "/help/:name" : "helpRoute", "/downloads/*path"

    : "downloadRoute" "*path" : "defaultRoute", }, defaultRoute: function(path){ alert(path); }, helpRoute: function(name){ … }, downloadRoute: function(path){ … }, }); var appController = new AppController; Backbone.history.start(); Controllers
  25. var BookView = Backbone.View.extend({ tagName: "li", className: "book", events: {

    "click .add": "addPage", "click .remove": "removePage", "hover .information": "showToolTip" }, initialize: function() { _.bindAll(this, "render", "addPage", "removePage", "showToolTip"); }, addPage: function() { … }, removePage: function() { … } }); Views
  26. var BookView = Backbone.View.extend({ tagName: 'li', className: 'book', bookTemplate: _.template("<strong><%=

    model.get('title') %></strong> <%= model.get('author') %> <span class='delete'>delete</span>"), initialize: function() { _.bindAll(this, 'render', 'clear', 'remove'); this.model.bind('remove', this.clear); }, events: { 'click .delete': 'remove' }, remove: function() { this.model.destroy(); }, render: function() { $(this.el).html(this.bookTemplate({ model: this.model })); return this; }, clear: function() { $(this.el).remove(); } });
  27. … events: { "click .add": "addPage", "click .remove": "removePage", "hover

    .information": "showToolTip" }, … Views evento .selector nombreFuncion
  28. mousedown mouseup mousemove mouseover keydown keyup keypress click dblclick focus

    focusin … hover custom events focusout blur Views jQuery v.1.6.1.
  29. Referencias / recursos Repositorio oficial https://github.com/documentcloud/backbone Documentación http://documentcloud.github.com/backbone/docs/backbone.html Backbone JS

    by Nick Gauthier http://ngauthier-backbone.heroku.com Backbone tutorials http://backbonetutorials.com Nailing the Interactions on Pageless Apps (with Backbone.js) http://vimeo.com/23948808 Re-using Backbone.js Models on the server with Node.js and Socket.io to build real-time apps http://andyet.net/blog/2011/feb/15/re-using-backbonejs-models-on-the-server- with-node Backbone.js tutorial http://www.plexical.com/blog/2010/11/18/backbone-js-tutorial
  30. Referencias / recursos Spine.js http://maccman.github.com/spine An Introduction to Spine.js http://addyosmani.com/blog/bloguilding-apps-spinejs

    Sproutcore http://www.sproutcore.com Backbone.js vs SproutCore http://news.ycombinator.com/item?id=2119704 Sproutcore vs jQuery +backbone.js http://net.tutsplus.com/tutorials/javascript-ajax/getting-started-with- backbone-js Knockout.js http://knockoutjs.com Knockout.js vs. Backbone.js http://www.ifandelse.com/?p=61 JavascriptMVC http://javascriptmvc.com/ <angular/> http://angularjs.org
  31. Esta obra se distribuye usando la licencia Creative Commons Reconocimiento-NoComercial-CompartirIgual

    3.0 Unported. Presentación realizada para el Meet App #8 del 17 de junio de 2011 en Madrid.