SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Backbone	
  Meetup	
  S01E01	
  
1	
  Backbone	
  Meetup	
  
C’est quoi ?
Ca apporte quoi ?
Disclaimer : cette présentation n’est pas exhaustive!
2	
  Backbone	
  Meetup	
  
Backbone
var	
  UneVueBackBone	
  =	
  Backbone.View.extend({	
  
	
  	
  	
  	
  template:	
  _.template($(’#template').html()),	
  
	
  	
  	
  	
  tagName	
  :	
  'tr',	
  
	
  	
  	
  	
  initialize:	
  function	
  (options)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  this.model	
  =	
  options.model;	
  
	
  	
  	
  	
  	
  	
  	
  	
  this.listenTo(this.model,	
  'change',	
  this.render);	
  
	
  	
  	
  	
  },	
  
	
  	
  	
  	
  destroy:	
  function	
  ()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  delete	
  this.model;	
  
	
  	
  	
  	
  },	
  
	
  	
  	
  	
  render:	
  function	
  ()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  this.$el.html(this.template(this.model.toJSON()));	
  
	
  	
  	
  	
  	
  	
  	
  	
  return	
  this;	
  
	
  	
  	
  	
  }	
  
});	
  
Marionette
var	
  UneVueMarionette	
  =	
  Backbone.Marionette.ItemView.extend({	
  
	
  	
  	
  	
  template:	
  ’#template',	
  
	
  	
  	
  	
  tagName	
  :	
  'tr',	
  
	
  	
  	
  	
  modelEvents	
  :	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  'change'	
  :	
  'render'	
  
	
  	
  	
  	
  }	
  
});	
  
Vues
3	
  Backbone	
  Meetup	
  
Vues	
  
•  ItemView
•  CollectionView
•  CompositeView
•  Régions et Layouts
4	
  Backbone	
  Meetup	
  
var	
  MyView	
  =	
  Backbone.Marionette.ItemView.extend({	
  
	
  	
  	
  	
  	
  template:	
  "#some-­‐template"	
  
});	
  
	
  
var	
  view	
  =	
  new	
  MyView({	
  model	
  :	
  monModel	
  });	
  
	
  
var	
  ColView	
  =	
  Backbone.Marionette.CollectionView.extend({	
  
	
  	
  	
  	
  itemView:	
  MyItemView	
  
});	
  
	
  
var	
  colview	
  =	
  new	
  ColView({	
  collection	
  :	
  maCollection});	
  
Var	
  CompoView	
  =	
  Backbone.Marionette.CompositeView.extend({	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  template:	
  '#some-­‐template',	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  itemView	
  :	
  MyItemView,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  itemViewContainer	
  :	
  'tbody	
  
	
  });	
  
	
  
var	
  compoView	
  =	
  new	
  CompoView({	
  model	
  :	
  m,	
  collection	
  :	
  c});	
   5	
  Backbone	
  Meetup	
  
Layout	
  et	
  régions	
  
<script	
  id="mon-­‐premier-­‐layout"	
  type="text/template">	
  
	
  	
  	
  	
  <div	
  id="bloc-­‐premier"></div>	
  
	
  	
  	
  	
  <div	
  id="bloc-­‐second"></div>	
  
</script>	
  
	
  
var	
  AppLayout	
  =	
  Backbone.Marionette.Layout.extend({	
  
	
  	
  template:	
  "#mon-­‐premier-­‐layout",	
  
	
  
	
  	
  regions:	
  {	
  
	
  	
  	
  	
  blocPremier:	
  "#bloc-­‐premier",	
  
	
  	
  	
  	
  blocSecond:	
  "#bloc-­‐second"	
  
	
  	
  }	
  
});	
  
	
  
	
  
...	
  
	
  
layout.blocPremier.show(maVuePremiere);	
  
layout.blocSecond.show(maSecondeVue);	
  
6	
  Backbone	
  Meetup	
  
Application
MyApp	
  =	
  new	
  Backbone.Marionette.Application();	
  
	
  
MyApp.addInitializer(function(options){	
  
	
  	
  	
  	
  var	
  myView	
  =	
  new	
  MyView({	
  
	
  	
  	
  	
  	
  	
  	
  	
  model:	
  options.someModel	
  
	
  	
  	
  	
  });	
  
	
  	
  	
  	
  MyApp.mainRegion.show(myView);	
  
});	
  
	
  
MyApp.addInitializer(function(options){	
  
	
  	
  	
  	
  new	
  MyAppRouter();	
  
	
  	
  	
  	
  Backbone.history.start();	
  
});	
  
•  Initializers
•  Regions
•  Events
	
  
MyApp.addRegions({	
  
	
  	
  	
  	
  mainRegion:	
  "#content"	
  
});	
  
	
  
MyApp.vent.on("monEvent",	
  function(data){	
  
	
  	
  alert("Received",	
  data);	
  
});	
  
	
  
MyApp.start();	
  	
  
7	
  Backbone	
  Meetup	
  
Du code!
(et pas juste copié de la doc!)
8	
  Backbone	
  Meetup	
  
Events and callback methods
•  onShow
•  onBeforeRender, onRender
•  onBeforeClose, onClose
Backbone.Marionette.ItemView.extend({	
  
	
  	
  onRender:	
  function(){	
  
	
  	
  	
  	
  //	
  manipulate	
  the	
  `el`	
  here.	
  it's	
  already	
  
	
  	
  	
  	
  //	
  been	
  rendered,	
  and	
  is	
  full	
  of	
  the	
  view's	
  
	
  	
  	
  	
  //	
  HTML,	
  ready	
  to	
  go.	
  
	
  	
  }	
  
});	
  
9	
  Backbone	
  Meetup	
  
Template dynamique
MyView	
  =	
  Backbone.Marionette.ItemView.extend({	
  
	
  	
  getTemplate:	
  function(){	
  
	
  	
  	
  	
  if	
  (this.model.get("foo")){	
  
	
  	
  	
  	
  	
  	
  return	
  "#some-­‐template";	
  
	
  	
  	
  	
  }	
  else	
  {	
  
	
  	
  	
  	
  	
  	
  return	
  "#a-­‐different-­‐template";	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  
});	
  
10	
  Backbone	
  Meetup	
  
Accès aux éléments du DOM
Backbone.Marionette.ItemView.extend({	
  
	
  	
  tagName:	
  "tr",	
  
	
  
	
  	
  ui:	
  {	
  
	
  	
  	
  	
  checkbox:	
  "input[type=checkbox]"	
  
	
  	
  },	
  
	
  
	
  	
  onRender:	
  function()	
  {	
  
	
  	
  	
  	
  if	
  (this.model.get('selected'))	
  {	
  
	
  	
  	
  	
  	
  	
  this.ui.checkbox.addClass('checked');	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  
});	
  
	
  
On s’en sert souvent
11	
  Backbone	
  Meetup	
  
Model events et collection events
Marionette.ItemView.extend({	
  
	
  	
  modelEvents:	
  {	
  //	
  Item	
  view	
  et	
  composite	
  view	
  
	
  	
  	
  	
  "change":	
  "modelChanged"	
  
	
  	
  },	
  
	
  
	
  	
  collectionEvents:	
  {	
  //	
  collection	
  view	
  et	
  composite	
  view	
  
	
  	
  	
  	
  "add":	
  "modelAdded"	
  
	
  	
  }	
  
});	
  
	
  
modelEvents:	
  {	
  
	
  	
  	
  	
  "change":	
  	
  "handler1	
  handler2"	
  //	
  On	
  peut	
  en	
  avoir	
  plusieurs	
  
},	
  
	
  
	
  
modelEvents:	
  {	
  
	
  	
  	
  	
  "change":	
  	
  function	
  ()	
  {	
  //	
  On	
  inliner	
  le	
  handler	
  
	
  	
  	
  	
  }	
  
},	
  
	
  
	
  
12	
  Backbone	
  Meetup	
  
Template Helpers
<script	
  id="meetup-­‐template"	
  type="text/html">	
  
	
  	
  Le	
  <%=	
  getName()	
  	
  %>	
  c’est	
  <%=	
  message()	
  %>	
  
</script>	
  
	
  
MyView	
  =	
  Backbone.Marionette.ItemView.extend({	
  
	
  	
  template:	
  "#meetup-­‐template",	
  
	
  
	
  	
  templateHelpers:	
  {	
  
	
  	
  	
  	
  getName:	
  function(){	
  
	
  	
  	
  	
  	
  	
  return	
  this.name	
  
	
  	
  	
  	
  },	
  
	
  	
  	
  	
  message:	
  function(){	
  
	
  	
  	
  	
  	
  	
  return	
  "	
  Génial!"	
  
	
  	
  	
  	
  }	
  
	
  
	
  	
  }	
  
	
  
});	
  
	
  
model	
  =	
  new	
  Backbone.Model({name:	
  	
  "Backbone	
  meetup"});	
  
view	
  =	
  new	
  MyView({	
  model:	
  model	
  });	
  
	
  
view.render();	
  //=>	
  	
  Le	
  Backbone	
  meetup	
  c’est	
  Génial!	
  	
  
	
   13	
  Backbone	
  Meetup	
  
Plus sur les CollectionView
buildItemView:	
  function(item,	
  ItemViewType,	
  itemViewOptions){	
  
	
  	
  	
  if	
  (item.get('someProperty'))	
  {	
  
	
  	
  	
  	
  	
  	
  return	
  new	
  MyViewUn();	
  
	
  	
  	
  }	
  
	
  	
  	
  return	
  new	
  MyView2();	
  
}	
  
Construction dynamique de l’item view
Vue particulière pour les collections vides
Backbone.Marionette.CollectionView.extend({	
  
	
  	
  emptyView:	
  NoItemsView	
  
});	
  
	
  
Evénements des enfants
itemEvents:	
  {	
  
	
  	
  "render":	
  function()	
  {	
  
	
  	
  	
  	
  	
  //	
  ...	
  
	
  	
  }	
  
}	
  
	
   14	
  Backbone	
  Meetup	
  
Encore	
  du	
  code!	
  	
  
	
  
15	
  Backbone	
  Meetup	
  
Et	
  aussi…	
  
16	
  Backbone	
  Meetup	
  
Behaviors	
  
	
  var	
  MyView	
  =	
  Marionette.ItemView.extend({	
  
	
  	
  	
  	
  behaviors:	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  ValidateDate:	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  message:	
  "La	
  date	
  de	
  naissance	
  n'est	
  pas	
  valide!"	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  }	
  
});	
  
	
  
	
  
var	
  ValidateDate	
  =	
  Marionette.Behavior.extend({	
  
	
  	
  	
  	
  defaults:	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  "message":	
  "Date	
  non	
  valide!"	
  
	
  	
  	
  	
  },	
  
	
  
	
  	
  	
  	
  //	
  behaviors	
  have	
  events	
  that	
  are	
  bound	
  to	
  the	
  views	
  DOM	
  
	
  	
  	
  	
  events:	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  "blur	
  input.date":	
  "validateDate"	
  
	
  	
  	
  	
  },	
  
	
  
	
  	
  	
  	
  validateDate:	
  function()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Validateion	
  de	
  la	
  date	
  et	
  ajout	
  du	
  message	
  dsi	
  nécessaire	
  
	
  	
  	
  	
  }	
  
});	
  
17	
  Backbone	
  Meetup	
  
A retenir
•  Marionette est une librairie pour backbone
•  Plus précisément, c’est un ensemble d’utilitaires
simplifiant la construction d’applications
•  En particulier Marionette fournie un ensemble
de vues très très pratiques
18	
  Backbone	
  Meetup	
  
19	
  Backbone	
  Meetup	
  
Le site : http://marionettejs.com
La Doc : https://github.com/marionettejs/backbone.marionette
Code de la démo : https://github.com/rlemaire/bb-meetup-marionette

Contenu connexe

Tendances

Skaters and BMXers from all over the U.S. descend on Grant Park
Skaters and BMXers from all over the U.S. descend on Grant ParkSkaters and BMXers from all over the U.S. descend on Grant Park
Skaters and BMXers from all over the U.S. descend on Grant Parkchicagonewsyesterday
 
Zend Framework: abordagem prática
Zend Framework: abordagem práticaZend Framework: abordagem prática
Zend Framework: abordagem práticaMarcelo Andrade
 
Javascript and jQuery for Mobile
Javascript and jQuery for MobileJavascript and jQuery for Mobile
Javascript and jQuery for MobileIvano Malavolta
 
Як досвід компанії перетворився на фреймворк
Як досвід компанії перетворився на фреймворкЯк досвід компанії перетворився на фреймворк
Як досвід компанії перетворився на фреймворкShtrih Sruleg
 
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...irwinvifxcfesre
 
Юнит тестирование в Zend Framework 2.0
Юнит тестирование в Zend Framework 2.0Юнит тестирование в Zend Framework 2.0
Юнит тестирование в Zend Framework 2.0zfconfua
 
Twig, los mejores trucos y técnicas avanzadas
Twig, los mejores trucos y técnicas avanzadasTwig, los mejores trucos y técnicas avanzadas
Twig, los mejores trucos y técnicas avanzadasJavier Eguiluz
 
Drupal Cms Prezentace
Drupal Cms PrezentaceDrupal Cms Prezentace
Drupal Cms PrezentaceTomáš Kafka
 
A slew of AACM 50th anniversary celebrations this weekend
A slew of AACM 50th anniversary celebrations this weekendA slew of AACM 50th anniversary celebrations this weekend
A slew of AACM 50th anniversary celebrations this weekendchicagonewsyesterday
 
JavascriptMVC
JavascriptMVCJavascriptMVC
JavascriptMVC4lb0
 
Chief Keef's hologram can't catch a break, and it's a win for Keef
Chief Keef's hologram can't catch a break, and it's a win for KeefChief Keef's hologram can't catch a break, and it's a win for Keef
Chief Keef's hologram can't catch a break, and it's a win for Keefchicagonewsonlineradio
 
Intro to jQuery UI
Intro to jQuery UIIntro to jQuery UI
Intro to jQuery UIappendTo
 
Zend bootstrap
Zend bootstrapZend bootstrap
Zend bootstrapneuros
 
Working With Ajax Frameworks
Working With Ajax FrameworksWorking With Ajax Frameworks
Working With Ajax FrameworksJonathan Snook
 
HTTP Interceptors com AngularJS
HTTP Interceptors com AngularJSHTTP Interceptors com AngularJS
HTTP Interceptors com AngularJSRodrigo Branas
 
Check out our photos of the Pixies' Metro show
Check out our photos of the Pixies' Metro showCheck out our photos of the Pixies' Metro show
Check out our photos of the Pixies' Metro showchicagonewsyesterday
 
Migrating Legacy Web Applications to AngularJS
Migrating Legacy Web Applications to AngularJSMigrating Legacy Web Applications to AngularJS
Migrating Legacy Web Applications to AngularJSBTI360
 
JavaScript de qualidade: hoje, amanhã e sempre!
JavaScript de qualidade: hoje, amanhã e sempre!JavaScript de qualidade: hoje, amanhã e sempre!
JavaScript de qualidade: hoje, amanhã e sempre!Thiago de Oliveira Pires
 

Tendances (20)

Skaters and BMXers from all over the U.S. descend on Grant Park
Skaters and BMXers from all over the U.S. descend on Grant ParkSkaters and BMXers from all over the U.S. descend on Grant Park
Skaters and BMXers from all over the U.S. descend on Grant Park
 
Zend Framework: abordagem prática
Zend Framework: abordagem práticaZend Framework: abordagem prática
Zend Framework: abordagem prática
 
Javascript and jQuery for Mobile
Javascript and jQuery for MobileJavascript and jQuery for Mobile
Javascript and jQuery for Mobile
 
Як досвід компанії перетворився на фреймворк
Як досвід компанії перетворився на фреймворкЯк досвід компанії перетворився на фреймворк
Як досвід компанії перетворився на фреймворк
 
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
Peek inside the fantastical Ukrainian Village home and studio of artists Jare...
 
Юнит тестирование в Zend Framework 2.0
Юнит тестирование в Zend Framework 2.0Юнит тестирование в Zend Framework 2.0
Юнит тестирование в Zend Framework 2.0
 
Twig, los mejores trucos y técnicas avanzadas
Twig, los mejores trucos y técnicas avanzadasTwig, los mejores trucos y técnicas avanzadas
Twig, los mejores trucos y técnicas avanzadas
 
Drupal Cms Prezentace
Drupal Cms PrezentaceDrupal Cms Prezentace
Drupal Cms Prezentace
 
A slew of AACM 50th anniversary celebrations this weekend
A slew of AACM 50th anniversary celebrations this weekendA slew of AACM 50th anniversary celebrations this weekend
A slew of AACM 50th anniversary celebrations this weekend
 
JavascriptMVC
JavascriptMVCJavascriptMVC
JavascriptMVC
 
Chief Keef's hologram can't catch a break, and it's a win for Keef
Chief Keef's hologram can't catch a break, and it's a win for KeefChief Keef's hologram can't catch a break, and it's a win for Keef
Chief Keef's hologram can't catch a break, and it's a win for Keef
 
Intro to jQuery UI
Intro to jQuery UIIntro to jQuery UI
Intro to jQuery UI
 
Zend bootstrap
Zend bootstrapZend bootstrap
Zend bootstrap
 
Working With Ajax Frameworks
Working With Ajax FrameworksWorking With Ajax Frameworks
Working With Ajax Frameworks
 
Jquery Framework
Jquery FrameworkJquery Framework
Jquery Framework
 
HTTP Interceptors com AngularJS
HTTP Interceptors com AngularJSHTTP Interceptors com AngularJS
HTTP Interceptors com AngularJS
 
Check out our photos of the Pixies' Metro show
Check out our photos of the Pixies' Metro showCheck out our photos of the Pixies' Metro show
Check out our photos of the Pixies' Metro show
 
Migrating Legacy Web Applications to AngularJS
Migrating Legacy Web Applications to AngularJSMigrating Legacy Web Applications to AngularJS
Migrating Legacy Web Applications to AngularJS
 
Index1
Index1Index1
Index1
 
JavaScript de qualidade: hoje, amanhã e sempre!
JavaScript de qualidade: hoje, amanhã e sempre!JavaScript de qualidade: hoje, amanhã e sempre!
JavaScript de qualidade: hoje, amanhã e sempre!
 

En vedette

Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.jsRoman Kalyakin
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.jsJonathan Weiss
 
Intro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General AssemblyIntro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General AssemblyAzat Mardanov
 
Backbone And Marionette : Take Over The World
Backbone And Marionette : Take Over The WorldBackbone And Marionette : Take Over The World
Backbone And Marionette : Take Over The Worldharshit040591
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsMark
 
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ... Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...Mikko Ohtamaa
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Andrii Lundiak
 
Backbone/Marionette introduction
Backbone/Marionette introductionBackbone/Marionette introduction
Backbone/Marionette introductionmatt-briggs
 
Introduction to Marionette Collective
Introduction to Marionette CollectiveIntroduction to Marionette Collective
Introduction to Marionette CollectivePuppet
 
Introduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsIntroduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsReturn on Intelligence
 
Client-side MVC with Backbone.js
Client-side MVC with Backbone.js Client-side MVC with Backbone.js
Client-side MVC with Backbone.js iloveigloo
 
introduction to Marionette.js (jscafe14)
introduction to Marionette.js (jscafe14)introduction to Marionette.js (jscafe14)
introduction to Marionette.js (jscafe14)Ryuma Tsukano
 
Backbone.js
Backbone.jsBackbone.js
Backbone.jsVO Tho
 
Marionette: the Backbone framework
Marionette: the Backbone frameworkMarionette: the Backbone framework
Marionette: the Backbone frameworkfrontendne
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.jsPragnesh Vaghela
 

En vedette (20)

Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.js
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.js
 
Intro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General AssemblyIntro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General Assembly
 
Backbone And Marionette : Take Over The World
Backbone And Marionette : Take Over The WorldBackbone And Marionette : Take Over The World
Backbone And Marionette : Take Over The World
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.js
 
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ... Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
 
MVC & backbone.js
MVC & backbone.jsMVC & backbone.js
MVC & backbone.js
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]
 
Backbone/Marionette introduction
Backbone/Marionette introductionBackbone/Marionette introduction
Backbone/Marionette introduction
 
Introduction to Marionette Collective
Introduction to Marionette CollectiveIntroduction to Marionette Collective
Introduction to Marionette Collective
 
Introduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsIntroduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.js
 
Marionette talk 2016
Marionette talk 2016Marionette talk 2016
Marionette talk 2016
 
Client-side MVC with Backbone.js
Client-side MVC with Backbone.js Client-side MVC with Backbone.js
Client-side MVC with Backbone.js
 
introduction to Marionette.js (jscafe14)
introduction to Marionette.js (jscafe14)introduction to Marionette.js (jscafe14)
introduction to Marionette.js (jscafe14)
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Marionette: the Backbone framework
Marionette: the Backbone frameworkMarionette: the Backbone framework
Marionette: the Backbone framework
 
RequireJS
RequireJSRequireJS
RequireJS
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.js
 
RequireJS
RequireJSRequireJS
RequireJS
 

Introduction à Marionette

  • 1. Backbone  Meetup  S01E01   1  Backbone  Meetup  
  • 2. C’est quoi ? Ca apporte quoi ? Disclaimer : cette présentation n’est pas exhaustive! 2  Backbone  Meetup  
  • 3. Backbone var  UneVueBackBone  =  Backbone.View.extend({          template:  _.template($(’#template').html()),          tagName  :  'tr',          initialize:  function  (options)  {                  this.model  =  options.model;                  this.listenTo(this.model,  'change',  this.render);          },          destroy:  function  ()  {                                  delete  this.model;          },          render:  function  ()  {                  this.$el.html(this.template(this.model.toJSON()));                  return  this;          }   });   Marionette var  UneVueMarionette  =  Backbone.Marionette.ItemView.extend({          template:  ’#template',          tagName  :  'tr',          modelEvents  :  {                    'change'  :  'render'          }   });   Vues 3  Backbone  Meetup  
  • 4. Vues   •  ItemView •  CollectionView •  CompositeView •  Régions et Layouts 4  Backbone  Meetup  
  • 5. var  MyView  =  Backbone.Marionette.ItemView.extend({            template:  "#some-­‐template"   });     var  view  =  new  MyView({  model  :  monModel  });     var  ColView  =  Backbone.Marionette.CollectionView.extend({          itemView:  MyItemView   });     var  colview  =  new  ColView({  collection  :  maCollection});   Var  CompoView  =  Backbone.Marionette.CompositeView.extend({                          template:  '#some-­‐template',                          itemView  :  MyItemView,                          itemViewContainer  :  'tbody    });     var  compoView  =  new  CompoView({  model  :  m,  collection  :  c});   5  Backbone  Meetup  
  • 6. Layout  et  régions   <script  id="mon-­‐premier-­‐layout"  type="text/template">          <div  id="bloc-­‐premier"></div>          <div  id="bloc-­‐second"></div>   </script>     var  AppLayout  =  Backbone.Marionette.Layout.extend({      template:  "#mon-­‐premier-­‐layout",        regions:  {          blocPremier:  "#bloc-­‐premier",          blocSecond:  "#bloc-­‐second"      }   });       ...     layout.blocPremier.show(maVuePremiere);   layout.blocSecond.show(maSecondeVue);   6  Backbone  Meetup  
  • 7. Application MyApp  =  new  Backbone.Marionette.Application();     MyApp.addInitializer(function(options){          var  myView  =  new  MyView({                  model:  options.someModel          });          MyApp.mainRegion.show(myView);   });     MyApp.addInitializer(function(options){          new  MyAppRouter();          Backbone.history.start();   });   •  Initializers •  Regions •  Events   MyApp.addRegions({          mainRegion:  "#content"   });     MyApp.vent.on("monEvent",  function(data){      alert("Received",  data);   });     MyApp.start();     7  Backbone  Meetup  
  • 8. Du code! (et pas juste copié de la doc!) 8  Backbone  Meetup  
  • 9. Events and callback methods •  onShow •  onBeforeRender, onRender •  onBeforeClose, onClose Backbone.Marionette.ItemView.extend({      onRender:  function(){          //  manipulate  the  `el`  here.  it's  already          //  been  rendered,  and  is  full  of  the  view's          //  HTML,  ready  to  go.      }   });   9  Backbone  Meetup  
  • 10. Template dynamique MyView  =  Backbone.Marionette.ItemView.extend({      getTemplate:  function(){          if  (this.model.get("foo")){              return  "#some-­‐template";          }  else  {              return  "#a-­‐different-­‐template";          }      }   });   10  Backbone  Meetup  
  • 11. Accès aux éléments du DOM Backbone.Marionette.ItemView.extend({      tagName:  "tr",        ui:  {          checkbox:  "input[type=checkbox]"      },        onRender:  function()  {          if  (this.model.get('selected'))  {              this.ui.checkbox.addClass('checked');          }      }   });     On s’en sert souvent 11  Backbone  Meetup  
  • 12. Model events et collection events Marionette.ItemView.extend({      modelEvents:  {  //  Item  view  et  composite  view          "change":  "modelChanged"      },        collectionEvents:  {  //  collection  view  et  composite  view          "add":  "modelAdded"      }   });     modelEvents:  {          "change":    "handler1  handler2"  //  On  peut  en  avoir  plusieurs   },       modelEvents:  {          "change":    function  ()  {  //  On  inliner  le  handler          }   },       12  Backbone  Meetup  
  • 13. Template Helpers <script  id="meetup-­‐template"  type="text/html">      Le  <%=  getName()    %>  c’est  <%=  message()  %>   </script>     MyView  =  Backbone.Marionette.ItemView.extend({      template:  "#meetup-­‐template",        templateHelpers:  {          getName:  function(){              return  this.name          },          message:  function(){              return  "  Génial!"          }        }     });     model  =  new  Backbone.Model({name:    "Backbone  meetup"});   view  =  new  MyView({  model:  model  });     view.render();  //=>    Le  Backbone  meetup  c’est  Génial!       13  Backbone  Meetup  
  • 14. Plus sur les CollectionView buildItemView:  function(item,  ItemViewType,  itemViewOptions){        if  (item.get('someProperty'))  {              return  new  MyViewUn();        }        return  new  MyView2();   }   Construction dynamique de l’item view Vue particulière pour les collections vides Backbone.Marionette.CollectionView.extend({      emptyView:  NoItemsView   });     Evénements des enfants itemEvents:  {      "render":  function()  {            //  ...      }   }     14  Backbone  Meetup  
  • 15. Encore  du  code!       15  Backbone  Meetup  
  • 16. Et  aussi…   16  Backbone  Meetup  
  • 17. Behaviors    var  MyView  =  Marionette.ItemView.extend({          behaviors:  {                  ValidateDate:  {                          message:  "La  date  de  naissance  n'est  pas  valide!"                  }          }   });       var  ValidateDate  =  Marionette.Behavior.extend({          defaults:  {                  "message":  "Date  non  valide!"          },            //  behaviors  have  events  that  are  bound  to  the  views  DOM          events:  {                  "blur  input.date":  "validateDate"          },            validateDate:  function()  {                  //  Validateion  de  la  date  et  ajout  du  message  dsi  nécessaire          }   });   17  Backbone  Meetup  
  • 18. A retenir •  Marionette est une librairie pour backbone •  Plus précisément, c’est un ensemble d’utilitaires simplifiant la construction d’applications •  En particulier Marionette fournie un ensemble de vues très très pratiques 18  Backbone  Meetup  
  • 19. 19  Backbone  Meetup   Le site : http://marionettejs.com La Doc : https://github.com/marionettejs/backbone.marionette Code de la démo : https://github.com/rlemaire/bb-meetup-marionette