SlideShare a Scribd company logo
HOW I LEARNED TO STOP FUCKING
AND LOVE MARIONETTEJS
MADE WITH ♥ BY KAMOL TREEWATCHARARAT
THE WHY
The reason behind this talk
been working with marionette almost a month

Lots of gotchas and caveats

sum up what I think worth sharing to the team
VIEW
View life cycle
new View()

initialize()

render()

template/getTemplate()

onRender()
TEMPLATE: ???
TEMPLATE: ???
• js function
• require(‘file.ejs’)
TEMPLATE: ???
• js function <— take JSON in, spit out HTML
• require(‘file.ejs’)
TEMPLATE: ???
• js function <— take JSON in, spit out HTML
• require(‘file.ejs’) <— what we’re using now
GETTEMPLATE VS TEMPLATE
?
GETTEMPLATE VS TEMPLATE
getTemplate gives you the view’s scope
?
Should I implement render() ?
Yes No
Should I implement render() ?
Vanilla Backbone View —> Yes
Marionette’s Views —> use onRender() instead
View life cycle (cont.)
destroy()

onBeforeDestroy()

onDestroy()

unbind all custom view events

unbind all DOM events

remove this.el from DOM

unbind all listenTo
Where do I put event listener?
• initialize
• index.js
• onRender
• in model
Where do I put event listener?
• initialize <— put in event hash instead!
• index.js
• onRender
• in model
Where do I put event listener?
• initialize <— put in event hash instead!
• index.js <— ideal for model-related events
• onRender
• in model
Example
exports.showPage = function () {
var view = new View();
var model = new Model();
model.on( 'change', … )
view.listenTo( model, 'change', … )
}
Where do I put event listener?
• initialize <— put in event hash instead!
• index.js <— ideal for model-related events
• onRender <— bad place
• in model
Where do I put event listener?
• initialize <— put in event hash instead!
• index.js <— ideal for model-related events
• onRender <— bad place
• in model <— nope
Life’s most persistent and urgent question is, “Do
you have any question?”
MODEL
Setup
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
model event
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
model.set( 'val1', {
test: 0
});
model event
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
model.set( 'val1', {
test: 0
});
Object { val1: { test: 0 } }
model event
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
var val1 = model.get( 'val1' );
val1.share = 1;
model.set( 'val1', val1 );
model event
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
var val1 = model.get( 'val1' );
val1.share = 1;
model.set( 'val1', val1 );
no trigger!!
model event
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
var val1 = model.get( 'val1' );
val1.share = 1;
model.set( 'val1', val1 );
model.trigger( 'change', model );
model event
var model = new Backbone.Model();
model.set( 'val1', {
like: 1,
love: 2,
share: 0
});
model.on( 'change', function ( thisModel ) {
console.log( thisModel.attributes );
});
var val1 = _.clone( model.get( 'val1' ) );
val1.share = 1;
model.set( 'val1', val1 );
But… the memory
alternatives: avoid nested
data
use sub-model

flatten them
What takes longer? Model.fetch() vs View.render()
?
What takes longer? Model.fetch() vs View.render()
Model.fetch()
?
Should we use
model.on( 'change', this.render );
?
Should we use
model.on( 'change', this.render );
?
No, use sandbox.showContent instead.
except for subviews
MARIONETTE’S VIEWS
ItemView
enhanced version of Backbone.View 

child view in a collection view

avoid adding stylesheet here
CollectionView
has no template ( you can put it in but it won’t get
called )

requires childView, and collection

can register childEvents

have to implement pagination events
CompositeView
inherit from CollectionView ( add in ItemView’s
features like template and stuffs )

is a hybrid of ItemView and CollectionView

require model, collection, childView
LayoutView
inherit from ItemView

has regions and regionManager
Q&A

More Related Content

Viewers also liked

Business development and market strategies
Business development and market strategiesBusiness development and market strategies
Business development and market strategies
Kunle Dosumu
 
Business development and market strategies
Business development and market strategiesBusiness development and market strategies
Business development and market strategies
Kunle Dosumu
 
Transcripts- Bachelor's degree in Management
Transcripts-  Bachelor's degree in ManagementTranscripts-  Bachelor's degree in Management
Transcripts- Bachelor's degree in ManagementLeonel Sanabria
 
Business value through requirements
Business value through requirementsBusiness value through requirements
Business value through requirements
Devon Goda
 
Starbucks store mgmt model transition tool brd
Starbucks store mgmt model transition tool brdStarbucks store mgmt model transition tool brd
Starbucks store mgmt model transition tool brd
Devon Goda
 
New TECNO Tablet Product 2016.
New TECNO Tablet Product 2016.New TECNO Tablet Product 2016.
New TECNO Tablet Product 2016.
Kunle Dosumu
 
Starbucks coffee company pay adjustment process
Starbucks coffee company   pay adjustment processStarbucks coffee company   pay adjustment process
Starbucks coffee company pay adjustment process
Devon Goda
 
Publicidad Low Cost
Publicidad Low CostPublicidad Low Cost
Publicidad Low Cost
Mario Zelada
 
Examenes de topografia
Examenes de topografiaExamenes de topografia
Examenes de topografia
Alfredo Leguia Guzman
 

Viewers also liked (11)

Business development and market strategies
Business development and market strategiesBusiness development and market strategies
Business development and market strategies
 
Business development and market strategies
Business development and market strategiesBusiness development and market strategies
Business development and market strategies
 
Uas
UasUas
Uas
 
ELIA Lyon 2015 Static
ELIA Lyon 2015 StaticELIA Lyon 2015 Static
ELIA Lyon 2015 Static
 
Transcripts- Bachelor's degree in Management
Transcripts-  Bachelor's degree in ManagementTranscripts-  Bachelor's degree in Management
Transcripts- Bachelor's degree in Management
 
Business value through requirements
Business value through requirementsBusiness value through requirements
Business value through requirements
 
Starbucks store mgmt model transition tool brd
Starbucks store mgmt model transition tool brdStarbucks store mgmt model transition tool brd
Starbucks store mgmt model transition tool brd
 
New TECNO Tablet Product 2016.
New TECNO Tablet Product 2016.New TECNO Tablet Product 2016.
New TECNO Tablet Product 2016.
 
Starbucks coffee company pay adjustment process
Starbucks coffee company   pay adjustment processStarbucks coffee company   pay adjustment process
Starbucks coffee company pay adjustment process
 
Publicidad Low Cost
Publicidad Low CostPublicidad Low Cost
Publicidad Low Cost
 
Examenes de topografia
Examenes de topografiaExamenes de topografia
Examenes de topografia
 

Similar to How I learned to stop fucking and love MarionetteJS

Javascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & AngularJavascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
Building mobile web apps with Mobello
Building mobile web apps with MobelloBuilding mobile web apps with Mobello
Building mobile web apps with Mobello
Jeong-Geun Kim
 
Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - Limenius
Ignacio Martín
 
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
🎤 Hanno Embregts 🎸
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksHjörtur Hilmarsson
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.js
Roman Kalyakin
 
Make your Backbone Application dance
Make your Backbone Application danceMake your Backbone Application dance
Make your Backbone Application dance
Nicholas Valbusa
 
Backbone.js Simple Tutorial
Backbone.js Simple TutorialBackbone.js Simple Tutorial
Backbone.js Simple Tutorial추근 문
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
Harvard Web Working Group
 
Clean Javascript
Clean JavascriptClean Javascript
Clean Javascript
Ryunosuke SATO
 
Ultimate Introduction To AngularJS
Ultimate Introduction To AngularJSUltimate Introduction To AngularJS
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
Jim Lynch
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
Bert Wijnants
 
Test Coverage in Rails
Test Coverage in RailsTest Coverage in Rails
Test Coverage in Rails
James Gray
 
Pattern Matching: Small Enhancement or Major Feature?
Pattern Matching: Small Enhancement or Major Feature?Pattern Matching: Small Enhancement or Major Feature?
Pattern Matching: Small Enhancement or Major Feature?
🎤 Hanno Embregts 🎸
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportAnton Arhipov
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach places
dn
 

Similar to How I learned to stop fucking and love MarionetteJS (20)

Javascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & AngularJavascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & Angular
 
Building mobile web apps with Mobello
Building mobile web apps with MobelloBuilding mobile web apps with Mobello
Building mobile web apps with Mobello
 
Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - Limenius
 
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
Pattern Matching: From Small Enhancement to Major Feature (talk from JavaLand...
 
Backbone Basics with Examples
Backbone Basics with ExamplesBackbone Basics with Examples
Backbone Basics with Examples
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
 
Introduction to Backbone.js
Introduction to Backbone.jsIntroduction to Backbone.js
Introduction to Backbone.js
 
Make your Backbone Application dance
Make your Backbone Application danceMake your Backbone Application dance
Make your Backbone Application dance
 
Backbone.js Simple Tutorial
Backbone.js Simple TutorialBackbone.js Simple Tutorial
Backbone.js Simple Tutorial
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Clean Javascript
Clean JavascriptClean Javascript
Clean Javascript
 
Ultimate Introduction To AngularJS
Ultimate Introduction To AngularJSUltimate Introduction To AngularJS
Ultimate Introduction To AngularJS
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
Test Coverage in Rails
Test Coverage in RailsTest Coverage in Rails
Test Coverage in Rails
 
Pattern Matching: Small Enhancement or Major Feature?
Pattern Matching: Small Enhancement or Major Feature?Pattern Matching: Small Enhancement or Major Feature?
Pattern Matching: Small Enhancement or Major Feature?
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience Report
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach places
 

Recently uploaded

National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 

Recently uploaded (20)

National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 

How I learned to stop fucking and love MarionetteJS

  • 1. HOW I LEARNED TO STOP FUCKING AND LOVE MARIONETTEJS MADE WITH ♥ BY KAMOL TREEWATCHARARAT
  • 3. The reason behind this talk been working with marionette almost a month Lots of gotchas and caveats sum up what I think worth sharing to the team
  • 5. View life cycle new View() initialize() render() template/getTemplate() onRender()
  • 7. TEMPLATE: ??? • js function • require(‘file.ejs’)
  • 8. TEMPLATE: ??? • js function <— take JSON in, spit out HTML • require(‘file.ejs’)
  • 9. TEMPLATE: ??? • js function <— take JSON in, spit out HTML • require(‘file.ejs’) <— what we’re using now
  • 11. GETTEMPLATE VS TEMPLATE getTemplate gives you the view’s scope ?
  • 12. Should I implement render() ? Yes No
  • 13. Should I implement render() ? Vanilla Backbone View —> Yes Marionette’s Views —> use onRender() instead
  • 14. View life cycle (cont.) destroy() onBeforeDestroy() onDestroy() unbind all custom view events unbind all DOM events remove this.el from DOM unbind all listenTo
  • 15. Where do I put event listener? • initialize • index.js • onRender • in model
  • 16. Where do I put event listener? • initialize <— put in event hash instead! • index.js • onRender • in model
  • 17. Where do I put event listener? • initialize <— put in event hash instead! • index.js <— ideal for model-related events • onRender • in model
  • 18. Example exports.showPage = function () { var view = new View(); var model = new Model(); model.on( 'change', … ) view.listenTo( model, 'change', … ) }
  • 19. Where do I put event listener? • initialize <— put in event hash instead! • index.js <— ideal for model-related events • onRender <— bad place • in model
  • 20. Where do I put event listener? • initialize <— put in event hash instead! • index.js <— ideal for model-related events • onRender <— bad place • in model <— nope
  • 21. Life’s most persistent and urgent question is, “Do you have any question?”
  • 22. MODEL
  • 23. Setup var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); });
  • 24. model event var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); }); model.set( 'val1', { test: 0 });
  • 25. model event var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); }); model.set( 'val1', { test: 0 }); Object { val1: { test: 0 } }
  • 26. model event var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); }); var val1 = model.get( 'val1' ); val1.share = 1; model.set( 'val1', val1 );
  • 27. model event var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); }); var val1 = model.get( 'val1' ); val1.share = 1; model.set( 'val1', val1 ); no trigger!!
  • 28. model event var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); }); var val1 = model.get( 'val1' ); val1.share = 1; model.set( 'val1', val1 ); model.trigger( 'change', model );
  • 29. model event var model = new Backbone.Model(); model.set( 'val1', { like: 1, love: 2, share: 0 }); model.on( 'change', function ( thisModel ) { console.log( thisModel.attributes ); }); var val1 = _.clone( model.get( 'val1' ) ); val1.share = 1; model.set( 'val1', val1 );
  • 31. alternatives: avoid nested data use sub-model flatten them
  • 32. What takes longer? Model.fetch() vs View.render() ?
  • 33. What takes longer? Model.fetch() vs View.render() Model.fetch() ?
  • 34. Should we use model.on( 'change', this.render ); ?
  • 35. Should we use model.on( 'change', this.render ); ? No, use sandbox.showContent instead. except for subviews
  • 37. ItemView enhanced version of Backbone.View child view in a collection view avoid adding stylesheet here
  • 38. CollectionView has no template ( you can put it in but it won’t get called ) requires childView, and collection can register childEvents have to implement pagination events
  • 39. CompositeView inherit from CollectionView ( add in ItemView’s features like template and stuffs ) is a hybrid of ItemView and CollectionView require model, collection, childView
  • 40. LayoutView inherit from ItemView has regions and regionManager
  • 41. Q&A