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

Backbone & Marionette - Ordena tu Javascript

Backbone & Marionette - Ordena tu Javascript

Charla sobre nuestra experiencia con Backbone y Marionette en CityHeroes. Ventajas, patrones, arquitectura.

Avatar for Mauro Trigo

Mauro Trigo

April 25, 2015
Tweet

More Decks by Mauro Trigo

Other Decks in Technology

Transcript

  1. WTF

  2. hola var Zoo = Backbone.Collection.extend({ model: Animal, url: '/zoo' });

    var zoologicoMunicipal = new Zoo([ { type: 'bufeo', sound: 'eee, eee' }, { type: 'tapir', sound: '???' }, ]); zoologicoMunicipal.get(0).shout(); // eee, eee Models / Collections var Animal = Backbone.Model.extend({ defaults: { type: 'shark', sound: 'roarr' }, shout: function() { alert(this.get('sound')); } }); // Herencia / Extensión var Cat = Animal.extend({ type: 'cat', sound: 'miau' }); var garfield = new Cat();
  3. Views var AnimalRow = Backbone.View.extend({ initialize: function() { this.listenTo( this.model,

    // manual data binding 'change', this.render ); }, tagName: 'li', template: _.template('...'), render: function() { this.$el.html( this.template(this.model.attributes) ); return this; } }); ... events: { 'dblclick': 'open', 'click .js-select': 'select' }, open: function() { window.open(this.model.get('public_url')); }, select: function() { this.model.set({selected: true}); } ...
  4. Un nuevo reto Single Page App? - API-centric company -

    JS > server-side code - Actions per minute++ - Load to dwell++
  5. Boilerplate code-- var AnimalRow = Backbone.View.extend({ initialize: function() { this.listenTo(

    this.model, // manual data binding 'change', this.render ); }, tagName: 'li', template: _.template('...'), render: function() { this.$el.html( this.template(this.model.attributes) ); return this; } }); var AnimalRow = Marionette.ItemView.extend({ tagName: 'li', template: _.template('...') }); // Booyakasha!
  6. - ItemView - CollectionView - CompositeView - LayoutView - Behaviors

    var AnimalRow = Marionette.ItemView.extend({ tagName: 'li', template: _.template('...') }); Marionette Views
  7. - ItemView - CollectionView - CompositeView - LayoutView - Behaviors

    var AnimalList = Marionette.CollectionView.extend({ tagName: 'ul', childView: AnimalRow }); var zoologicoMunicipal = new Zoo([ { type: 'bufeo', sound: 'eee, eee' }, { type: 'tapir', sound: '???' }, ]); var zooList = new AnimalList({ collection: zoologicoMunicipal }); Marionette Views
  8. - ItemView - CollectionView - CompositeView - LayoutView - Behaviors

    CollectionView + model, template Marionette Views
  9. - ItemView - CollectionView - CompositeView - LayoutView - Behaviors

    Marionette Views Behaviors.Printable = Marionette.Behavior.extend({ ui: { printableArea: '.js-printable-area' }, events: { 'click .js-print': 'print' }, print: function() { var printOptions = { mode : 'iframe', popClose : close, retainAttr : true }; this.ui.printableArea.printArea(printOptions); } });
  10. App.module('MiModulo', function(MiModulo, App) { // Funcionalidad del módulo aquí });

    Módulos - Modelos - Colecciones - Vistas - Eventos - Routing
  11. Eventos - Events - Commands - Requests - Channels trigger

    / listenTo command / comply request / reply var notificationsChannel = Backbone.Radio.channel('notif');
  12. - Contenedor del código - Startup var app = new

    Marionette.Application(); Aplicación
  13. Stack en CityHeroes - Backbone - Marionette - RequireJS -

    Bootstrap - Handlebars - Moment - Backbone Associations - Grunt - Gitflow - Trello - JS Hint
  14. Inspiración, fuentes 1. Amy Palamountain Unsuck Your Backbone 2. David

    Sulc Marionette Book Series 3. Addy Osmani Patterns For Large-Scale JavaScript Application Architecture 4. Laurie Voss ‘Stuff Everybody Knows Except You’