SlideShare a Scribd company logo
Knockoutjs – Components
2014/10/03 Howard
Registering components as a viewmodel/template pair
 The component name can be any nonempty string.
 It’s recommended, but not mandatory, to use lowercase dash-separated
strings (such as your-component-name) so that the component name is
valid to use as a custom element (such as <your-component-name>).
 viewModel is optional, and can take any of the viewModel formats
described below.
 template is required, and can take any of the template formats
described below.
Specifying a viewmodel
 A constructor function
 Knockout will invoke your constructor once for each instance of the
component, producing a separate viewmodel object for each.
 Properties on the resulting object or its prototype chain (e.g.,
someProperty and doSomething in the example above) are available
for binding in the component’s view.
Specifying a viewmodel
 A createViewModel factory function
 If you want to run any setup logic on the associated element
before it is bound to the viewmodel, or use arbitrary logic to
decide which viewmodel class to instantiate:
Specifying a viewmodel
 An AMD module whose value describes a viewmodel
 If you have an AMD loader (such as require.js) already in
your page, then you can use it to fetch a viewmodel
Specifying a template
 An existing element ID
 For example, the following element:
 an be used as the template for a component by specifying
its ID:
 You’re not limited to using <template> elements, but these
are convenient (on browsers that support them) since they
don’t get rendered on their own. Any other element type
works too.
Specifying a template
 An existing element instance
 If you have a reference to a DOM element in your code, you
can use it as a container for template markup:
Specifying a template
 A string of markup
Specifying a template
 An array of DOM nodes
 If you’re building configurations programmatically and you have an
array of DOM nodes, you can use them as a component template:
 In this case, all the specified nodes (and their descendants) will be
cloned and concatenated into each copy of the component that gets
instantiated.
The "component" binding
There are two ways to use the component binding:
 Shorthand syntax
 Full syntax
 name — the name of the component to inject. Again, this can be
observable.
 params — an object that will be passed on to the component.
Component lifecycle
1. Your component loaders are asked to supply the viewmodel
factory and template
2. The component template is cloned and injected into the
container element
3. If the component has a viewmodel, it is instantiated
4. The viewmodel is bound to the view
5. The component is active
6. The component is torn down, and the viewmodel is
disposed
Custom elements
Introduction
 Custom elements are a syntactical alternative to the
component binding.
<div data-bind='component: {
name: "flight-deals",
params: { from: "lhr", to: "sfo" }
}'></div>
 <flight-deals params='from: "lhr", to: "sfo"'></flight-deals>
Controlling custom element tag names
 If you want to have different custom element tag names, you can
override getComponentNameForNode to control this. For
example
References
 http://knockoutjs.com/documentation/component-
overview.html

More Related Content

PDF
Knockout.js components&routing
PPTX
Angular 2
PDF
AngularJS - dependency injection
PDF
.NET Core, ASP.NET Core Course, Session 11
PPTX
01 introduction to struts2
PPT
Struts2
PDF
.NET Core, ASP.NET Core Course, Session 13
PDF
.NET Core, ASP.NET Core Course, Session 14
Knockout.js components&routing
Angular 2
AngularJS - dependency injection
.NET Core, ASP.NET Core Course, Session 11
01 introduction to struts2
Struts2
.NET Core, ASP.NET Core Course, Session 13
.NET Core, ASP.NET Core Course, Session 14

What's hot (20)

PDF
Spring annotation
PDF
Dependency injection with koin
PDF
Angular 2 binding
ODP
Annotation-Based Spring Portlet MVC
PPTX
Desing pattern prototype-Factory Method, Prototype and Builder
PPTX
Spring from a to Z
PPTX
Angular Presentation
ODT
Types of Dependency Injection in Spring
PPTX
Services Factory Provider Value Constant - AngularJS
PPT
Struts,Jsp,Servlet
PPTX
Spring framework Controllers and Annotations
PDF
.NET Core, ASP.NET Core Course, Session 15
PPTX
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
PPTX
Spring core
PPSX
Spring - Part 1 - IoC, Di and Beans
ODP
Oro Workflows
PDF
Struts2
PDF
.NET Core, ASP.NET Core Course, Session 16
PDF
.NET Core, ASP.NET Core Course, Session 10
PPTX
Junit in mule demo
Spring annotation
Dependency injection with koin
Angular 2 binding
Annotation-Based Spring Portlet MVC
Desing pattern prototype-Factory Method, Prototype and Builder
Spring from a to Z
Angular Presentation
Types of Dependency Injection in Spring
Services Factory Provider Value Constant - AngularJS
Struts,Jsp,Servlet
Spring framework Controllers and Annotations
.NET Core, ASP.NET Core Course, Session 15
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Spring core
Spring - Part 1 - IoC, Di and Beans
Oro Workflows
Struts2
.NET Core, ASP.NET Core Course, Session 16
.NET Core, ASP.NET Core Course, Session 10
Junit in mule demo
Ad

Viewers also liked (18)

PPTX
Ian 20151002 es2015
PPTX
Vic weekly learning_20160504
PPTX
JavaScript
PPTX
Type script
PPTX
ES2015 promise
PPTX
20150313 ian git
PPT
Knockout
PDF
Asp.net MVC DI
PPTX
Howard type script
PPTX
JavsScript OOP
PPTX
Report introduction to_npm_gulp_bower
PPTX
Meteor + Ionic Introduction
PDF
Node js
PPTX
Peggy sass vs scss
PPTX
Node child process
PPTX
PPT
Ken 20150306 心得分享
PPTX
git command
Ian 20151002 es2015
Vic weekly learning_20160504
JavaScript
Type script
ES2015 promise
20150313 ian git
Knockout
Asp.net MVC DI
Howard type script
JavsScript OOP
Report introduction to_npm_gulp_bower
Meteor + Ionic Introduction
Node js
Peggy sass vs scss
Node child process
Ken 20150306 心得分享
git command
Ad

Similar to Knockoutjs – components (20)

PPT
Knockout Advanced Concepts By Surekha Gadkari
PDF
Knockout js session
PPTX
Fundaments of Knockout js
PDF
Knockoutjs databinding
PPTX
Knockout.js
PPTX
Knockout implementing mvvm in java script with knockout
PDF
Knockout in action
PPT
KnockoutJS and MVVM
PPTX
PPTX
Bringing the light to the client with KnockoutJS
PPTX
Knockout js
PPTX
MVVM Magic in SharePoint 2010 using Knockoutjs!
PPTX
Knockoutjs UG meeting presentation
PDF
Knockout mvvm-m5-slides
PDF
Knockout Introduction
PPTX
Knockout js
PPTX
XAML/C# to HTML5/JS
PPTX
Knockout js
KEY
Knockout.js presentation
PDF
Knockoutjs
Knockout Advanced Concepts By Surekha Gadkari
Knockout js session
Fundaments of Knockout js
Knockoutjs databinding
Knockout.js
Knockout implementing mvvm in java script with knockout
Knockout in action
KnockoutJS and MVVM
Bringing the light to the client with KnockoutJS
Knockout js
MVVM Magic in SharePoint 2010 using Knockoutjs!
Knockoutjs UG meeting presentation
Knockout mvvm-m5-slides
Knockout Introduction
Knockout js
XAML/C# to HTML5/JS
Knockout js
Knockout.js presentation
Knockoutjs

More from LearningTech (20)

PPTX
PPTX
PostCss
PPTX
ReactJs
PPTX
Docker
PPTX
Semantic ui
PPTX
node.js errors
PPTX
Process control nodejs
PPTX
Expression tree
PPTX
SQL 效能調校
PPTX
flexbox report
PPTX
Reflection &amp; activator
PPTX
Peggy markdown
PPTX
20160415ken.lee
PPTX
Peggy elasticsearch應用
PPTX
Expression tree
PPTX
Vic weekly learning_20160325
PPTX
D3js learning tips
PPTX
Vic weekly learning_20151127
PPTX
Mocha.js
PPTX
R language
PostCss
ReactJs
Docker
Semantic ui
node.js errors
Process control nodejs
Expression tree
SQL 效能調校
flexbox report
Reflection &amp; activator
Peggy markdown
20160415ken.lee
Peggy elasticsearch應用
Expression tree
Vic weekly learning_20160325
D3js learning tips
Vic weekly learning_20151127
Mocha.js
R language

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Electronic commerce courselecture one. Pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation theory and applications.pdf
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MIND Revenue Release Quarter 2 2025 Press Release
sap open course for s4hana steps from ECC to s4
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Reach Out and Touch Someone: Haptics and Empathic Computing
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation theory and applications.pdf

Knockoutjs – components

  • 2. Registering components as a viewmodel/template pair  The component name can be any nonempty string.  It’s recommended, but not mandatory, to use lowercase dash-separated strings (such as your-component-name) so that the component name is valid to use as a custom element (such as <your-component-name>).  viewModel is optional, and can take any of the viewModel formats described below.  template is required, and can take any of the template formats described below.
  • 3. Specifying a viewmodel  A constructor function  Knockout will invoke your constructor once for each instance of the component, producing a separate viewmodel object for each.  Properties on the resulting object or its prototype chain (e.g., someProperty and doSomething in the example above) are available for binding in the component’s view.
  • 4. Specifying a viewmodel  A createViewModel factory function  If you want to run any setup logic on the associated element before it is bound to the viewmodel, or use arbitrary logic to decide which viewmodel class to instantiate:
  • 5. Specifying a viewmodel  An AMD module whose value describes a viewmodel  If you have an AMD loader (such as require.js) already in your page, then you can use it to fetch a viewmodel
  • 6. Specifying a template  An existing element ID  For example, the following element:  an be used as the template for a component by specifying its ID:  You’re not limited to using <template> elements, but these are convenient (on browsers that support them) since they don’t get rendered on their own. Any other element type works too.
  • 7. Specifying a template  An existing element instance  If you have a reference to a DOM element in your code, you can use it as a container for template markup:
  • 8. Specifying a template  A string of markup
  • 9. Specifying a template  An array of DOM nodes  If you’re building configurations programmatically and you have an array of DOM nodes, you can use them as a component template:  In this case, all the specified nodes (and their descendants) will be cloned and concatenated into each copy of the component that gets instantiated.
  • 11. There are two ways to use the component binding:  Shorthand syntax  Full syntax  name — the name of the component to inject. Again, this can be observable.  params — an object that will be passed on to the component.
  • 12. Component lifecycle 1. Your component loaders are asked to supply the viewmodel factory and template 2. The component template is cloned and injected into the container element 3. If the component has a viewmodel, it is instantiated 4. The viewmodel is bound to the view 5. The component is active 6. The component is torn down, and the viewmodel is disposed
  • 14. Introduction  Custom elements are a syntactical alternative to the component binding. <div data-bind='component: { name: "flight-deals", params: { from: "lhr", to: "sfo" } }'></div>  <flight-deals params='from: "lhr", to: "sfo"'></flight-deals>
  • 15. Controlling custom element tag names  If you want to have different custom element tag names, you can override getComponentNameForNode to control this. For example