SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
MILANO 1863
POLITECNICO
Knockout.js
COMPONENTS & ROUTING
Carlo Bernaschina – carlo.bernaschina@polimi.it
In plain HTML we end up writing long and repetitive
structures.
CSSs can help reducing styling rules to simple Tag
classes.
Still in non naïve situations the
structure can be confusing.
The Problem
HTML TEMPLATES ORGANIZATION
We have HTML elements that hide complex structures.
Here are some examples:
• SELECT
• File INPUT
• VIDEO
The Question
HTML TEMPLATES ORGANIZATION
The SELECT element hides the definition of a complex
and structured UI.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
The Question
HTML ELEMENTS
The File INPUT element hides the definition of a
complex UI and behavior.
<input type="file">
The Question
HTML ELEMENTS
The VIDEO element hides the definition of a complex
structured UI and behavior.
<video width="320" height="240" controls>
<source src="movie.ogg" type="video/ogg">
</video>
The Question
HTML ELEMENTS
Can we define our own custom elements?
The Question
HTML CUSTOM ELEMENTS
Yes We Can
Defining custom elements we can organize the
structure of an application in a more clean way.
<main-menu></main-menu>
<todo-list></todo-list>
A Solution
HTML CUSTOM ELEMENTS
In Knockout.js it is possible to define components.
“Components are a powerful, clean way of organizing
your UI code into self-contained, reusable chunks.”
These allow the developer to better structure both the
Views and the View-Models of the application.
Knockout.js
COMPONENTS
In Knockout.js new components are defined in the
following way.
ko.components.register(<the component name>, {
template: <the template used in the component>
});
The component name is any non empty string.
The template describes the HTML structure that will be
used to render the component.
Components
THE DEFINITION
The Template of the component can be passed to the
register function in may ways:
• Template ID
• String
• …
Components
THE DEFINITION - TEMPLATE
The Template of the component can be a template ID.
In HTML it is possible to define templates in the following way.
<template id="my-component-template">
<h1>Title</h1>
<button>
Click me right now
</button>
</template>
In the register function it can be passed as follow.
template:{element: 'my-component-template'}
Components
THE DEFINITION - TEMPLATE
The Template of the component can be a string.
'<h1>Title</h1><button>Click me right now</button>'
In the register function it can be passed as follow.
template: '<h1>Title</h1> … </button>'
Components
THE DEFINITION - TEMPLATE
The Component can have an optional ViewModel that
allow one to define custom properties and events.
ko.components.register(<the component name>, {
template: <the template used in the component>
viewModel: <ViewModel constructor>
});
Components
THE DEFINITION - VIEWMODEL
Once the Component has been defined it can be used
via the component binding.
<div data-bind='component: "<name>"'></div>
Components
THE DEFINITION - USAGE
It is possible to pass data to the component using a
params property in the component binding.
<div data-bind='component: {
name: "<name>",
params: { … }
}'></div>
Components
THE DEFINITION - PARAMS
The defined parameters are the first argument passed
to the ViewModel constructor
function ViewModel(params) {
…
}
Components
THE DEFINITION - PARAMS
In Knockout.js it is possible to include components
into the views using custom elements.
“Custom elements are an optional but convenient
syntax for consuming components.”
These make the Views more readable and self
explaining.
Knockout.js
CUSTOM ELEMENTS
A custom element can be included into the HTML as a
normal element.
<div data-bind='component: {
name: "<component-name>",
params: { … }
}'></div>
Becomes
<component-name params=" … "></component-name>
Knockout.js
COMPONENTS - USAGE
A SPA (Single Page Application) is an application
developed with client-side technologies that, instead of
reloading the page when navigating from one area to
another, dynamically changes the structure of the page to
reflect the navigation.
In SPAs a common problem is Routing.
We would like keep the URL-based nature of the web while
redirecting requests to different URLs to different areas of
the same application.
Routing
SINGLE PAGE APPLICATIONS
For Knockout.js is available a library that exploits
components for manage routing.
ko-component-router
https://github.com/caseyWebb/ko-component-router
Routing
A COMPONENT BASED APPROACH
ko-component-router is a library based on page.js that
organizes the areas of the SPA in components.
Each component describes a different area.
The navigation from one area to another triggers the
disposal of the active component and the instantiation of
the component responsible of the target one.
Routing
A COMPONENT BASED APPROACH
In a SPA based on ko-component-router the area that will
be updated on navigation is marked by a particular
element.
<ko-component-router></ko-component-router>
The initialization of the router is done by
ko.router.start();
Routing
INITIALIZATION
Components describing an application area are defined as
usual.
The only difference is in the ViewModel initialization where
instead of the params object a context is passed.
The context represents the current state of the application.
function HomeViewModel(ctx) {
…
}
Routing
DEFINE COMPONENTS
A Route is an access point for the application.
It is defined by the Path and the component that will
manage the matching requests.
ko.router.route(<path>, <component>)
Example
ko.router.route('/home', 'Home')
Routing
DEFINE ROUTES
What can be used as a Path?
A Path can be a simple string describing the address.
'/some/folder/in/the/path‘
It can contain Wildcards
'/*‘
It can be a Regular Expression
/^/commits/(d+)..(d+)/
Routing
DEFINE ROUTES
In the path of a page we can define Parameters.
A Parameter is a part of the path that can match a string.
The current value of the parameter is passed to the
Component ViewModel.
ko.router.route('/user/:user_id', 'UserDetails')
The parameter is accessible from the context
function UserDetailsViewModel(ctx) {
… ctx.params.user_id …
}
Routing
ROUTE PARAMETERS
While normal links can be used it is better to use the
custom route binding.
<a href="/user/1234">John Doe</a>
Becomes
<a data-bind="route: '/user/1234'">John Doe</a>
This allow one to compute the target route dynamically as it
can be computed based on Knockout.js Observables.
Routing
NAVIGATION
• http://www.html5rocks.com/en/tutorials/webcompon
ents/customelements/
• http://knockoutjs.com/documentation
• https://github.com/caseyWebb/ko-component-router
• https://visionmedia.github.io/page.js/
Reference

Más contenido relacionado

La actualidad más candente

Gender and pop culture
Gender and pop cultureGender and pop culture
Gender and pop culture
HUM116Fall2010
 
Queer theory powerpoint presentation october 2014
Queer theory powerpoint presentation   october 2014Queer theory powerpoint presentation   october 2014
Queer theory powerpoint presentation october 2014
Caryl Dolinko
 
literary theories and approaches simplified version
literary theories and approaches simplified versionliterary theories and approaches simplified version
literary theories and approaches simplified version
spartanako06
 
What is orientalism
What is orientalismWhat is orientalism
What is orientalism
ankimakwana
 

La actualidad más candente (20)

Paper no :-8 THE CULTURAL STUDIES New historicism
Paper no :-8 THE CULTURAL STUDIES New historicismPaper no :-8 THE CULTURAL STUDIES New historicism
Paper no :-8 THE CULTURAL STUDIES New historicism
 
Introduction to Literature
Introduction to LiteratureIntroduction to Literature
Introduction to Literature
 
Gender and pop culture
Gender and pop cultureGender and pop culture
Gender and pop culture
 
Queer theory powerpoint presentation october 2014
Queer theory powerpoint presentation   october 2014Queer theory powerpoint presentation   october 2014
Queer theory powerpoint presentation october 2014
 
Feminist theory
Feminist theoryFeminist theory
Feminist theory
 
literary theories and approaches simplified version
literary theories and approaches simplified versionliterary theories and approaches simplified version
literary theories and approaches simplified version
 
Feminism (Feminist Critical Approach)
Feminism (Feminist Critical Approach)Feminism (Feminist Critical Approach)
Feminism (Feminist Critical Approach)
 
Paper 9...presentation
Paper 9...presentationPaper 9...presentation
Paper 9...presentation
 
GRADE 6-8.pptx
GRADE 6-8.pptxGRADE 6-8.pptx
GRADE 6-8.pptx
 
French structuralism
French structuralismFrench structuralism
French structuralism
 
What is orientalism
What is orientalismWhat is orientalism
What is orientalism
 
Cultural Materialism
Cultural MaterialismCultural Materialism
Cultural Materialism
 
Feminism
FeminismFeminism
Feminism
 
Intertextuality
Intertextuality Intertextuality
Intertextuality
 
Gender Critcism
Gender CritcismGender Critcism
Gender Critcism
 
Deconstruction by MARIA MUMTAZ
Deconstruction by MARIA MUMTAZDeconstruction by MARIA MUMTAZ
Deconstruction by MARIA MUMTAZ
 
Major Characteristics of the 19th Century English Novel
Major Characteristics of the 19th Century English Novel   Major Characteristics of the 19th Century English Novel
Major Characteristics of the 19th Century English Novel
 
The narrative paradigm
The narrative paradigm The narrative paradigm
The narrative paradigm
 
Transcendentalism and american literature
Transcendentalism and american literatureTranscendentalism and american literature
Transcendentalism and american literature
 
The Importance of Being Earnest
The Importance of Being EarnestThe Importance of Being Earnest
The Importance of Being Earnest
 

Similar a Knockout.js components&routing

Architecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolArchitecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling Tool
Adriaan Venter
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvc
Guo Albert
 

Similar a Knockout.js components&routing (20)

Web components - An Introduction
Web components - An IntroductionWeb components - An Introduction
Web components - An Introduction
 
Architecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolArchitecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling Tool
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Unit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptxUnit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptx
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
Directives
DirectivesDirectives
Directives
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJS
 
Building and Deployment of Drupal sites with Features and Context
Building and Deployment of Drupal sites with Features and ContextBuilding and Deployment of Drupal sites with Features and Context
Building and Deployment of Drupal sites with Features and Context
 
Code igniter - A brief introduction
Code igniter - A brief introductionCode igniter - A brief introduction
Code igniter - A brief introduction
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1
 
HTML5 Semantics
HTML5 SemanticsHTML5 Semantics
HTML5 Semantics
 
Angular 2
Angular 2Angular 2
Angular 2
 
Milwaukee JS - Live binding with CanJS
Milwaukee JS - Live binding with CanJSMilwaukee JS - Live binding with CanJS
Milwaukee JS - Live binding with CanJS
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
17612235.ppt
17612235.ppt17612235.ppt
17612235.ppt
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvc
 
Google Polymer Introduction
Google Polymer IntroductionGoogle Polymer Introduction
Google Polymer Introduction
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Ionic tabs template explained
Ionic tabs template explainedIonic tabs template explained
Ionic tabs template explained
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Último (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Knockout.js components&routing

  • 1. MILANO 1863 POLITECNICO Knockout.js COMPONENTS & ROUTING Carlo Bernaschina – carlo.bernaschina@polimi.it
  • 2. In plain HTML we end up writing long and repetitive structures. CSSs can help reducing styling rules to simple Tag classes. Still in non naïve situations the structure can be confusing. The Problem HTML TEMPLATES ORGANIZATION
  • 3. We have HTML elements that hide complex structures. Here are some examples: • SELECT • File INPUT • VIDEO The Question HTML TEMPLATES ORGANIZATION
  • 4. The SELECT element hides the definition of a complex and structured UI. <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> The Question HTML ELEMENTS
  • 5. The File INPUT element hides the definition of a complex UI and behavior. <input type="file"> The Question HTML ELEMENTS
  • 6. The VIDEO element hides the definition of a complex structured UI and behavior. <video width="320" height="240" controls> <source src="movie.ogg" type="video/ogg"> </video> The Question HTML ELEMENTS
  • 7. Can we define our own custom elements? The Question HTML CUSTOM ELEMENTS
  • 8. Yes We Can Defining custom elements we can organize the structure of an application in a more clean way. <main-menu></main-menu> <todo-list></todo-list> A Solution HTML CUSTOM ELEMENTS
  • 9. In Knockout.js it is possible to define components. “Components are a powerful, clean way of organizing your UI code into self-contained, reusable chunks.” These allow the developer to better structure both the Views and the View-Models of the application. Knockout.js COMPONENTS
  • 10. In Knockout.js new components are defined in the following way. ko.components.register(<the component name>, { template: <the template used in the component> }); The component name is any non empty string. The template describes the HTML structure that will be used to render the component. Components THE DEFINITION
  • 11. The Template of the component can be passed to the register function in may ways: • Template ID • String • … Components THE DEFINITION - TEMPLATE
  • 12. The Template of the component can be a template ID. In HTML it is possible to define templates in the following way. <template id="my-component-template"> <h1>Title</h1> <button> Click me right now </button> </template> In the register function it can be passed as follow. template:{element: 'my-component-template'} Components THE DEFINITION - TEMPLATE
  • 13. The Template of the component can be a string. '<h1>Title</h1><button>Click me right now</button>' In the register function it can be passed as follow. template: '<h1>Title</h1> … </button>' Components THE DEFINITION - TEMPLATE
  • 14. The Component can have an optional ViewModel that allow one to define custom properties and events. ko.components.register(<the component name>, { template: <the template used in the component> viewModel: <ViewModel constructor> }); Components THE DEFINITION - VIEWMODEL
  • 15. Once the Component has been defined it can be used via the component binding. <div data-bind='component: "<name>"'></div> Components THE DEFINITION - USAGE
  • 16. It is possible to pass data to the component using a params property in the component binding. <div data-bind='component: { name: "<name>", params: { … } }'></div> Components THE DEFINITION - PARAMS
  • 17. The defined parameters are the first argument passed to the ViewModel constructor function ViewModel(params) { … } Components THE DEFINITION - PARAMS
  • 18. In Knockout.js it is possible to include components into the views using custom elements. “Custom elements are an optional but convenient syntax for consuming components.” These make the Views more readable and self explaining. Knockout.js CUSTOM ELEMENTS
  • 19. A custom element can be included into the HTML as a normal element. <div data-bind='component: { name: "<component-name>", params: { … } }'></div> Becomes <component-name params=" … "></component-name> Knockout.js COMPONENTS - USAGE
  • 20. A SPA (Single Page Application) is an application developed with client-side technologies that, instead of reloading the page when navigating from one area to another, dynamically changes the structure of the page to reflect the navigation. In SPAs a common problem is Routing. We would like keep the URL-based nature of the web while redirecting requests to different URLs to different areas of the same application. Routing SINGLE PAGE APPLICATIONS
  • 21. For Knockout.js is available a library that exploits components for manage routing. ko-component-router https://github.com/caseyWebb/ko-component-router Routing A COMPONENT BASED APPROACH
  • 22. ko-component-router is a library based on page.js that organizes the areas of the SPA in components. Each component describes a different area. The navigation from one area to another triggers the disposal of the active component and the instantiation of the component responsible of the target one. Routing A COMPONENT BASED APPROACH
  • 23. In a SPA based on ko-component-router the area that will be updated on navigation is marked by a particular element. <ko-component-router></ko-component-router> The initialization of the router is done by ko.router.start(); Routing INITIALIZATION
  • 24. Components describing an application area are defined as usual. The only difference is in the ViewModel initialization where instead of the params object a context is passed. The context represents the current state of the application. function HomeViewModel(ctx) { … } Routing DEFINE COMPONENTS
  • 25. A Route is an access point for the application. It is defined by the Path and the component that will manage the matching requests. ko.router.route(<path>, <component>) Example ko.router.route('/home', 'Home') Routing DEFINE ROUTES
  • 26. What can be used as a Path? A Path can be a simple string describing the address. '/some/folder/in/the/path‘ It can contain Wildcards '/*‘ It can be a Regular Expression /^/commits/(d+)..(d+)/ Routing DEFINE ROUTES
  • 27. In the path of a page we can define Parameters. A Parameter is a part of the path that can match a string. The current value of the parameter is passed to the Component ViewModel. ko.router.route('/user/:user_id', 'UserDetails') The parameter is accessible from the context function UserDetailsViewModel(ctx) { … ctx.params.user_id … } Routing ROUTE PARAMETERS
  • 28. While normal links can be used it is better to use the custom route binding. <a href="/user/1234">John Doe</a> Becomes <a data-bind="route: '/user/1234'">John Doe</a> This allow one to compute the target route dynamically as it can be computed based on Knockout.js Observables. Routing NAVIGATION
  • 29. • http://www.html5rocks.com/en/tutorials/webcompon ents/customelements/ • http://knockoutjs.com/documentation • https://github.com/caseyWebb/ko-component-router • https://visionmedia.github.io/page.js/ Reference