Skip to content

Instantly share code, notes, and snippets.

@noroot
Created August 16, 2014 07:47
Show Gist options
  • Save noroot/f094258eaa80e26a5ae3 to your computer and use it in GitHub Desktop.
Save noroot/f094258eaa80e26a5ae3 to your computer and use it in GitHub Desktop.
ExtJS 5 ViewModel
Ext.define('TestViewModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.test', // connects to viewModel/type below
data: {
firstName: 'John',
lastName: 'Doe'
},
formulas: {
// We'll explain formulas in more detail soon.
name: function (get) {
var fn = get('firstName'), ln = get('lastName');
return (fn && ln) ? (fn + ' ' + ln) : (fn || ln || '');
}
}
});
Ext.define('TestView', {
extend: 'Ext.panel.Panel',
layout: 'form',
// Always use this form when defining a view class. This
// allows the creator of the component to pass data without
// erasing the ViewModel type that we want.
viewModel: {
type: 'test' // references alias "viewmodel.test"
},
bind: {
title: 'Hello {name}'
},
defaultType: 'textfield',
items: [{
fieldLabel: 'First Name',
bind: '{firstName}'
},{
fieldLabel: 'Last Name',
bind: '{lastName}'
},{
xtype: 'button',
text: 'Submit',
bind: {
hidden: '{!name}'
}
}]
});
Ext.onReady(function () {
Ext.create('TestView', {
renderTo: Ext.getBody(),
width: 400
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment