SlideShare a Scribd company logo
View models and binding
Sencha Inc. ©2015
ViewModels
and Data Binding
Evan Trimboli
Sr. Software Engineer, Sencha
Sencha Inc. ©2015
Why ViewModels
•Automate connection between data and view
• Keep view in sync with data
• Update data with user input
•Declarative instead of imperative
Sencha Inc. ©2015
Sencha Inc. ©2015
MVVM
View ViewModel Model
Business Logic
and
Data
Presentation and view logic
Binding
Binding
Sencha Inc. ©2015
Binding Essentials
•The “bind descriptor” is a value that describes
the desired data.
•The “bound value” is the desired data delivered
by the ViewModel once it becomes available.
•The bound value will be delivered again
whenever it changes.
Sencha Inc. ©2015
Direct Binding
var vm = new Ext.app.ViewModel();
var b = vm.bind('{foo}', function (v) {
console.log(v);
});
vm.set('foo', 42); // b.setValue(‘42’);
b.destroy();
1
2
3
Bind
descriptor
Bound
value
> 42
Binding
instance
4
Marks
dependencies
as dirty
Sencha Inc. ©2015
Negated Binding
vm.bind('{!x}', function (v) {
console.log(v);
});
vm.set('x', 'world');
Bind
descriptor
> false
Sencha Inc. ©2015
Binding Templates
vm.bind('Hello {x:capitalize}', function (v) {
console.log(v);
});
vm.set('x', 'world');
Bind
descriptor
> Hello World
Sencha Inc. ©2015
Objects and Arrays
vm.bind({ x: 'x={x}', y: ['{y}'] }, function (v) {
console.log(v);
});
vm.set('x', 42);
// wait for it...
vm.set('y', 13);
Bind
descriptor
> { x: 'x=42', y: [13] }
Sencha Inc. ©2015
Bind Options
vm.set(‘foo’, {bar: 1});
vm.bind('{foo}', this.onFoo, this, {
deep: true,
single: true,
twoWay: false
});
// Without deep, foo won’t change
vm.set(‘foo.bar’, 100);
Deliver
changes to
child object
properties
Deliver just the
initial value
(once available)
Don’t write-back
changes, mark
binding readOnly
Sencha Inc. ©2015
Bind Concepts in Depth
•Bind descriptor – the data you want.
•Bind token – the replaceable parts (“{foo}”) of a
bind descriptor (single name or dot path).
•Dot path – the traversal of objects and properties
starting at the root of the ViewModel.
•Bound value – the value delivered by the
ViewModel as the result of a bind request.
Sencha Inc. ©2015
Bind Descriptor
•String with one token (“{foo}”) or direct
bind.
•String template (“Hello {foo}”).
•Object whose values are bind descriptors.
{foo: “{bar}”}
•Array whose elements are bind descriptors.
[“{foo}”, “{bar}”]
Sencha Inc. ©2015
Bind Tokens
•Simple identifiers (“{foo}”) to select top-level
values in the ViewModel.
•A “dot-path” to the desired value (such as
“{ticket.reporter.name}”).
•Formatters such as “Hi {foo:capitalize}”
or “Amount {num:round(2)}”.
•Negated token (“{!foo}”).
Sencha Inc. ©2015
Dot Path
• Normal object property.
• Field from a data record (“{user.name}” not
“{user.data.name}”).
• The associated record of a “to-one” association
(“{order.user.name}”).
• The associated store of a “to many” association
(“{user.orders}”).
Sencha Inc. ©2015
Bound Value
• The exact value from the VM if a “direct bind” (just
one token).
• The boolean negation of the value if a negated direct
bind (such as “{!foo}”).
• A string with all tokens replaced and formatted (such
as “{num:round(2)}”).
• An object or array with all property values or
elements replaced by their bound value.
• A store or record instance.
Sencha Inc. ©2015
Components
and
ViewModels
Sencha Inc. ©2015
Config Binding
Ext.define('App.view.Main', {
...
items: [{
xtype: 'button',
bind: {
text: 'Sign out, {user.name}'
}
}]
});
bind
config
Sencha Inc. ©2015
Config Binding Options
Ext.define('App.view.Main', {
...
items: [{
xtype: 'button',
bind: {
text: {
bindTo: '{foo}',
single: true
}
}
}]
});
Bind
descriptor
Bind
options
Sencha Inc. ©2015
defaultBindProperty
[{
xtype: ‘button’,
bind: ‘{theText}’ // text
}, {
xtype: ‘textfield’,
bind: ‘{theValue}’ // value
}, {
xtype: ‘grid’,
bind: ‘{theStore}’ // store
}]
Sencha Inc. ©2015
•Components have a default bind property, which
maps to the most common usage for that
component
Two Way Binding
var p = new Ext.panel.Panel({
bind: {
title: ‘{theTitle}’
},
viewModel: {
data: {theTitle: ‘Foo’}
}
});
// Not twoWayBindable, won’t update VM
p.setTitle(‘Bar’);
Sencha Inc. ©2015
•Configs that cannot be changed by the user are
typically not two way bindable by default.
What ViewModel does that bind use?
Sencha Inc. ©2015
Component Tree
viewport
panel
toolbar
button
grid
panel
panel
toolbar
button
viewport
panel
toolbar
button
grid
panel
panel
toolbar
button
ViewModel
ViewModel
Sencha Inc. ©2015
ViewModel Inheritance
viewport
panel
panel
toolbar
button {
bind: '{foo}'
}
ViewModel
...
Sencha Inc. ©2015
ViewModel
Sencha Inc. ©2015
ViewModel Hierarchy
ViewModel
ViewModel
{ }
{ }
data
prototype
data
parent
Sencha Inc. ©2015
Populating ViewModels
•Direct set() calls
•Configs
• data
• stores
• formulas
• links
Sencha Inc. ©2015
Data
Sencha Inc. ©2015
ViewModel – Data
Ext.define('App.view.main.Main’,{
extend: 'Ext.panel.Panel',
xtype: 'main',
viewModel: {
type: 'main',
data: {
foo: 42
}
}
});
Ext.define('App.view.main.MainModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.main',
data: {
foo: 427,
bar: ‘baz’
}
});
Sencha Inc. ©2015
ViewModel – Data (2)
Ext.create({
xtype: 'main',
viewModel: {
data: {
foo: 42
}
}
});
ViewModel
type comes
from the class
Sencha Inc. ©2015
Data is
merged, “bar”
still exists
Stores
Sencha Inc. ©2015
ViewModel – Stores
Ext.define('App.view.main.MainModel’, {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.main',
stores: {
bar: {
model: 'User’,
filters: [{
property: 'name',
value: '{form.search}'
}]
}
...
Object is a bind
descriptor
Sencha Inc. ©2015
ViewModel – Store Types
Ext.define('App.view.main.MainModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.main',
stores: {
bar: {
type: 'personnel',
...
}
}
});
Ext.define('App.store.Personnel', {
extend: 'Ext.data.Store',
alias: 'store.personnel',
proxy: ...
});
Sencha Inc. ©2015
Formulas
Sencha Inc. ©2015
ViewModel – Formulas
Ext.define('App.view.main.MainModel', {
...
formulas: {
twice: function (get) {
return 2 * get('x');
},
quad: function (get) {
return 2 * get('twice');
}
Use the
getter
Formulas can
use each other
Sencha Inc. ©2015
ViewModel – Two-Way Formulas
Ext.define('App.view.main.MainModel', {
...
formulas: {
twice: {
get: function (get) {
return 2 * get('x');
},
set: function (v) {
this.set('x', v / 2);
}
}
Called when
dependency
changes
this == VM
Sencha Inc. ©2015
Links
Sencha Inc. ©2015
ViewModel – Links
Ext.define('App.view.main.MainModel', {
...
links: {
ticket: {
type: 'Ticket',
id: 427
}
Load record
or request
from Session
Sencha Inc. ©2015
ViewModel – Links
Ext.create('App.view.form.Form', {
...
viewModel: {
links: {
ticket: theTicketInstance,
user: {
type: ‘User’,
create: {
name: ‘Foo Bar’
}
}
}
}
}
Already
loaded but
may copy
Sencha Inc. ©2015
Create a new
instance. May
also be true to
create empty
record
ViewModel – Links (Ext JS 6)
Ext.define('App.view.main.MainModel', {
...
links: {
foo: {
a: '{some.object.foo}',
b: '{other.thing.bar}'
}
}
}
Object is a bind
descriptor
Sencha Inc. ©2015
The Scheduler
Sencha Inc. ©2015
Shared Scheduler
ViewModel
ViewModel
parent
Scheduler
Sencha Inc. ©2015
Scheduler
Binding[0]
items
Binding[X]…
Data dependencies
Execution
Scheduler
Sencha Inc. ©2015
Running The Scheduler
vm.bind(’Hello {type} {thing:capitalize}', function (v) {
console.log(v);
});
vm.set(‘thing', 'world');
vm.set(‘type’, ‘cool’);
vm.notify();
Update all dirty
bound values
Sencha Inc. ©2015
Questions & Answers
Thank YOU!
Sencha Inc. ©2015

More Related Content

PPTX
Shaping Up Theme Roller Beyond Universal Theme
PPTX
Dynamic package loading 
and routing with Ext JS
PPTX
Introduction to spring boot
PPTX
Express JS
PDF
Spring MVC Framework
PDF
React Js Simplified
PPTX
reactJS
PDF
Flutter
Shaping Up Theme Roller Beyond Universal Theme
Dynamic package loading 
and routing with Ext JS
Introduction to spring boot
Express JS
Spring MVC Framework
React Js Simplified
reactJS
Flutter

What's hot (20)

PDF
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
PDF
Java script
PPTX
An Intro into webpack
PDF
Talend ESB : Monitoring, Repartition de Charge et Authentification
PDF
Asynchronous JavaScript Programming
PDF
introduction to Vue.js 3
PDF
Lets make a better react form
PPTX
Oracle application express ppt
PDF
Laravel Introduction
PPTX
Node js introduction
PPTX
Clean Code II - Dependency Injection
PPTX
Introduction to flutter's basic concepts
PDF
ES6 presentation
PDF
Asp.net caching
PPTX
Spring Boot Tutorial
PPTX
Flutter vs React Native Development in 2020
PDF
Introduction to Spring Boot!
PDF
Connecting Connect with Spring Boot
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
Java script
An Intro into webpack
Talend ESB : Monitoring, Repartition de Charge et Authentification
Asynchronous JavaScript Programming
introduction to Vue.js 3
Lets make a better react form
Oracle application express ppt
Laravel Introduction
Node js introduction
Clean Code II - Dependency Injection
Introduction to flutter's basic concepts
ES6 presentation
Asp.net caching
Spring Boot Tutorial
Flutter vs React Native Development in 2020
Introduction to Spring Boot!
Connecting Connect with Spring Boot
Ad

Similar to View models and binding (20)

PDF
Knockoutjs databinding
PPT
KnockoutJS and MVVM
PPTX
PPS
Knockout.js with ASP.Net Web API
PPTX
MVVM Magic in SharePoint 2010 using Knockoutjs!
PPTX
Knockout implementing mvvm in java script with knockout
PPTX
Knockout.js
PPTX
Knockout js
PPTX
Bringing the light to the client with KnockoutJS
PPTX
Sencha ExtJs Learning Part 2 - MVC And MVVM Architecture in ExtJs [ENGLISH]
ODP
ExtJS framework
PPTX
Building databound JavaScript apps with Knockoutjs
PPTX
Fundaments of Knockout js
KEY
Sugarcoating your frontend one ViewModel at a time
PPTX
Ext JS Introduction
PPTX
Mvvm and KnockoutJS
PPTX
Building Rich Internet Applications with Ext JS
PDF
Knockout js session
PPTX
Sencha Touch - Introduction
PDF
Rich Internet Applications con JavaFX e NetBeans
Knockoutjs databinding
KnockoutJS and MVVM
Knockout.js with ASP.Net Web API
MVVM Magic in SharePoint 2010 using Knockoutjs!
Knockout implementing mvvm in java script with knockout
Knockout.js
Knockout js
Bringing the light to the client with KnockoutJS
Sencha ExtJs Learning Part 2 - MVC And MVVM Architecture in ExtJs [ENGLISH]
ExtJS framework
Building databound JavaScript apps with Knockoutjs
Fundaments of Knockout js
Sugarcoating your frontend one ViewModel at a time
Ext JS Introduction
Mvvm and KnockoutJS
Building Rich Internet Applications with Ext JS
Knockout js session
Sencha Touch - Introduction
Rich Internet Applications con JavaFX e NetBeans
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
System and Network Administration Chapter 2
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Introduction to Artificial Intelligence
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
medical staffing services at VALiNTRY
Navsoft: AI-Powered Business Solutions & Custom Software Development
System and Network Administration Chapter 2
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Reimagine Home Health with the Power of Agentic AI​
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
VVF-Customer-Presentation2025-Ver1.9.pptx
top salesforce developer skills in 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Softaken Excel to vCard Converter Software.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How to Choose the Right IT Partner for Your Business in Malaysia

View models and binding

Editor's Notes

  • #5: Why is MVVM popular? Components act like a “virtual DOM”
  • #6: Rather than focus on MVVM architecture we’ll focus on VM and bind Flashlight in hand, we heading down to the lower-levels
  • #7: Start by how data is consumed from the VM
  • #8: Bind requests are asynchronous Not typically a one-time request
  • #9: A “direct” binding is the simplest Just a single value Can be two-way bound
  • #10: Bind tokens can be negated Super common need No other algebra at this time Formulas for more advanced
  • #19: How do components use this bind mechanism?
  • #20: Components use a bind config to update configs The bind config Keyed by config name Value is a bind descriptor
  • #21: Can provide an object with options, bindTo the descriptor
  • #22: defaultBindProperty chooses (one of) the most common properties of a component to make binding simpler
  • #23: Most configs are not two way bindable by default because they can’t be changed by the user.
  • #25: Lets see how the VM is determined View Models are owned by components They are inherited by descendants
  • #26: Component’s – lookupViewModel (the getter is ViewController – getViewModel
  • #27: We’ve seen how to use VM’s now let’s look at how to populate them
  • #28: View Models have “data” objects (like Data Models) The data objects are prototype chained
  • #30: We’ve seen how to use VM’s now let’s look at how to populate them
  • #31: Data declared on the VM is useful for default values Data declared on the View can be helpful if VM’s are reused
  • #32: Very often data must be supplied when a view is created ViewModel type default is important – let the class decide its VM type!
  • #33: We’ve seen how to use VM’s now let’s look at how to populate them
  • #34: Any store config is valid
  • #35: The type from the store comes from the alias
  • #36: Formulas allow us to do more complex things with view models
  • #37: Using the getter creates a dependency
  • #38: Scope chain behavior here! Must be deterministic, see example
  • #39: Links are used to load individual records
  • #40: type, id are required, will request from the session or server
  • #41: Useful for child sessions If the record is from a different (parent) session
  • #42: Reuse complex bindings
  • #43: We’ve seen how to use VM’s now let’s look at how to populate them
  • #44: The scheduler keeps a ordered list of bindings Bindings often bind to remove one level of indirection
  • #45: The scheduler keeps a ordered list of bindings Bindings often bind to remove one level of indirection