sencha touch 2.. accessing config values while creating views - sencha-touch

I have created a view like this
Ext.define('App.view.Message', {
extend: 'Ext.Panel',
alias: 'widget.message',
config: {
layout: 'vbox',
bigText: 'big Text',
smallText: 'small text',
imageUrl: 'imageurl',
oT: '',
description: 'message description',
items: [
{
flex: 3,
xtype: 'container',
layout: 'hbox',
items: [
{
xtype: 'container',
flex: 1,
items: [
{
html: '<h3>' + this.getBigText() + '</h3>'
},
{
html: '<h5>' + this.getSmallText() + '</h5>'
}
]
},
{
xtype: 'image',
src: this.getImageUrl(),
flex: 1
}
]
},
{
flex: 6,
xtype: 'container',
layout: 'vbox',
items: [
{
flex:1,
html: '<h3>' + this.getBigText() + '</h3>'
},
{
flex:5,
html: '<p>'+this.getDescription()+'</p>'
}
]
},
{
flex: 1,
xtype: 'button',
text: this.getOT()
}
]
}
})
i need to access values that will be passed when this view is created (config values) while creating the view.
so statement this.getDescription(), this.getBigText() etc etc are generating errors..
what i want is tht the view should be rendered with the config values that i pass is when im creating the view..
what should i do?

Looks like you have to override the constructor and get the config values:
Ext.define('App.view.Message', {
extend: 'Ext.Panel',
alias: 'widget.message',
config: {
layout: 'vbox',
bigText: 'big Text',
smallText: 'small text',
imageUrl: 'imageurl',
oT: '',
description: 'message description'
},
constructor: function(config)
{
config.items = [
{
flex: 3,
xtype: 'container',
layout: 'hbox',
items: [
{
xtype: 'container',
flex: 1,
items: [
{
html: '<h3>' + config.bigText + '</h3>'
},
{
html: '<h5>' + config.smallText + '</h5>'
}
]
},
{
xtype: 'image',
src: config.imageUrl,
flex: 1
}
]
},
{
flex: 6,
xtype: 'container',
layout: 'vbox',
items: [
{
flex:1,
html: '<h3>' + config.bigText + '</h3>'
},
{
flex:5,
html: '<p>'+config.description+'</p>'
}
]
},
{
flex: 1,
xtype: 'button',
text: config.oT
}
];
this.callParent(arguments);
}
});

Sencha Touch 2 does not automatically generate getters and setters for your config. However, there are 2 ways to do what you need:
Get & set config values directly through Ext.getCmp('your_component_id').config.bigText (similar to other configs)
Manually create getters & setters for your custom config, for example, you have to define something like this as a config of your view component:
getBigText: function(){return this.bigText;}
setBigText: function(value) {this.bigText = value;}
Hope this helps.

When you are define and load a "View" file - browser go through each line. The "View" instance isn't created at that time. So, naturally the this cannot be found there.
However, you can use "this" inside the initialize function. So, you can write your code like this:
Ext.define('App.view.Message', {
extend: 'Ext.Panel',
xtype: 'message',
config: {
layout: 'vbox',
bigText: 'big Text',
smallText: 'small text',
imageUrl: 'imageurl',
oT: '',
description: 'message description'
},
initialize : function(){
this.add([
{
flex: 3,
xtype: 'container',
layout: 'hbox',
items: [
{
xtype: 'container',
flex: 1,
items: [
{
html: '<h3>' + this.bigText + '</h3>'
},
{
html: '<h5>' + this.smallText + '</h5>'
}
]
},
{
xtype: 'image',
src: this.imageUrl,
flex: 1
}
]
},
{
flex: 6,
xtype: 'container',
layout: 'vbox',
items: [
{
flex:1,
html: '<h3>' + this.bigText + '</h3>'
},
{
flex:5,
html: '<p>'+ this.description +'</p>'
}
]
},
{
flex: 1,
xtype: 'button',
text: this.oT
}
]);
this.callParent(arguments);
}
});

For that I recommend that you add you items to the view in the initialize function like so :
...
config:{
...
},
initialize: function(){
var me = this;
me.callParent();
var container = Ext.create('Ext.Container',{
title = this.config.bigText
});
me.add(container);
}
...
I don't know if there is a best solution but this works.

A simple solution is to do that in initialize() function, eg.
this.config.title = 'my title';
this.config.iconCls = 'info';
this.callParent();

Related

Sencha Touch: Navigation View ui item won't change

I am trying to set the 'ui' property of my navigation view to light, but it only shows in the dark theme. Here's my view. What am I missing?
Ext.define('VisitTCMIndy.view.Social', {
extend: 'Ext.navigation.View',
xtype: 'socialcard',
config: {
iconCls: 'chat',
title: 'Social',
layout: 'card',
ui: 'light',
items: [
{
xtype: 'panel',
title: 'Social',
layout: 'vbox',
items: [
{
xtype: 'panel',
layout: 'hbox',
flex: 1,
items: [
{
xtype: 'image',
baseCls: 'socialbutton',
title: 'Facebook',
src: "resources/images/facebookbutton.png",
flex: 1
},
{
xtype: 'image',
baseCls: 'socialbutton',
title: 'Twitter',
src: "resources/images/twitterbutton.png",
flex: 1
}
]
},
{
xtype: 'panel',
layout: 'hbox',
flex: 1,
items: [
{
xtype: 'image',
baseCls: 'socialbutton',
title: 'Pinterest',
src: "resources/images/pinterestbutton.png",
flex: 1
},
{
xtype: 'image',
baseCls: 'socialbutton',
title: 'Instagram',
src: "resources/images/instagrambutton.png",
flex: 1
}
]
},
{
xtype: 'button',
text: 'Take a Picture',
style: 'margin:3%'
}
]
}
]
}
});
If you mean that you want to change the ui of the navigation bar of the navigation view, then should do this:
config: {
iconCls: 'chat',
title: 'Social',
layout: 'card',
navigationBar: {
ui: 'dark'
}
...
Hope this helps

Re-sizing form content to fit browser window - Using Card Layout

I need the tab-panels to fit browser window size. I have used card layout, and it is not re-sizing to fit the browser window. I think i'm missing some properties in my viewPort.
launch: function() {
Ext.create('Ext.container.Viewport', {
layout: 'card',
items: [
{
xtype: 'panel',
items: { xtype: 'tabP1' }
},
{
xtype: 'panel',
items: { xtype:'tabP2' }
}
,
{
xtype: 'panel',
items: { xtype:'tabP3' }
}
]
});
},
2.) One of my tabpanels; I am using absolute layout. I am using this layout because it's easy to set form components where i ever i desire it to be. Therefore, i would like a solution that works with the same layout.
Ext.define('MyApp.view.MyForm', {
extend: 'Ext.form.Panel',
alias:'widget.tabP1',
// height: 250,
// width: 726,
layout: {
type: 'absolute'
},
bodyPadding: 10,
title: 'My Form',
initComponent: function() {
var me = this;
Ext.applyIf(me, {
items: [
{
xtype: 'gridpanel',
title: 'My Grid Panel',
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'string',
text: 'String'
},
{
xtype: 'numbercolumn',
dataIndex: 'number',
text: 'Number'
},
{
xtype: 'datecolumn',
dataIndex: 'date',
text: 'Date'
},
{
xtype: 'booleancolumn',
dataIndex: 'bool',
text: 'Boolean'
}
],
viewConfig: {
}
}
]
});
me.callParent(arguments);
}
});
UPDATE 2
UPDATE 2
launch: function() {
Ext.create('Ext.container.Viewport', {
layout: 'card',
items: [
{
xtype: 'tabP1'
},
{
xtype:'tabP2'
}
,
{
xtype:'tabP3'
}
]
});
},
onSuccess: function() {
this.getViewport().getLayout().setActiveItem(1);
},
I get an error when i use your code, saying that this.getViewport().getLayout().setActiveItem(1) is not a function. I think this is because i used border layout. How can i resolve this ?
Your problem is "over-nesting" Why are you putting 'tabPFoo' inside a panel with no layout? They are at best, redundant, also causing the layout system to spend more cycles when it's not needed.
Ext.require('*');
Ext.onReady(function(){
var vp = new Ext.container.Viewport({
layout: 'card',
items: [{
xtype: 'grid',
store: {
fields: ['name'],
data: [{
name: 'Name 1'
}]
},
columns: [{
flex: 1,
dataIndex: 'name',
text: 'Name'
}],
listeners: {
itemclick: function(view){
var grid = view.ownerCt,
next = grid.nextSibling();
vp.getLayout().setActiveItem(next);
}
}
}, {
xtype: 'grid',
store: {
fields: ['name'],
data: [{
name: 'Name 2'
}]
},
columns: [{
flex: 1,
dataIndex: 'name',
text: 'Name'
}],
listeners: {
itemclick: function(view){
var grid = view.ownerCt,
next = grid.nextSibling();
vp.getLayout().setActiveItem(next);
}
}
}, {
xtype: 'grid',
store: {
fields: ['name'],
data: [{
name: 'Name 3'
}]
},
columns: [{
flex: 1,
dataIndex: 'name',
text: 'Name'
}],
listeners: {
itemclick: function(view){
var grid = view.ownerCt,
next = grid.previousSibling().previousSibling();
vp.getLayout().setActiveItem(next);
}
}
}]
});
});
Obviously that's not MVC style, but that's the structure the layout should take.

Changing the Detail Container in a NestedList?

In a small program that I have, I'm trying to have custom containers for every leaf node of my Nested List.
Here are a couple arbitrary example containers that I'm trying to test with:
Ext.define('DetailContainer1', {
extend: 'Ext.Container',
xtype: 'detail-container1',
layout: {
type: 'vbox'
},
height: 300,
style: 'background-color: #a9a9a9;',
// flex: 1,
items: [{
html: 'Detail Container1'
}]
});
Ext.define('DetailContainer2', {
extend: 'Ext.Container',
xtype: 'detail-container2',
layout: {
type: 'vbox'
},
height: 300,
style: 'background-color: #c9c9c9;',
// flex: 1,
items: [{
html: 'Detail Container2',
flex: 1
}, {
xtype: 'button',
text: 'Hit me!',
flex: 1
}]
});
How can I switch in the new containers when the user clicks on the leaf node?
This action should happen in onLeafItemTap(). Btw the initial container (#2) isn't showing at the moment. Is this a layout issue?
Here's some idea of what the screen should look like:
Complete source:
Ext.Loader.setConfig({
enabled: true
});
Ext.define('DetailContainer1', {
extend: 'Ext.Container',
xtype: 'detail-container1',
layout: {
type: 'vbox'
},
height: 300,
style: 'background-color: #a9a9a9;',
// flex: 1,
items: [{
html: 'Detail Container1'
}]
});
Ext.define('DetailContainer2', {
extend: 'Ext.Container',
xtype: 'detail-container2',
layout: {
type: 'vbox'
},
height: 300,
style: 'background-color: #c9c9c9;',
// flex: 1,
items: [{
html: 'Detail Container2',
flex: 1
}, {
xtype: 'button',
text: 'Hit me!',
flex: 1
}]
});
Ext.define('ListItem', {
extend: 'Ext.data.Model',
config: {
fields: ['text']
}
});
Ext.define('Sencha.view.MainView', {
extend: 'Ext.Container',
xtype: 'mainview',
layout: {
type: 'hbox'
},
initialize: function() {
this.treeStore = Ext.create('Ext.data.TreeStore', {
model: 'ListItem',
defaultRootProperty: 'items',
root: {
items: [{
text: 'Containers',
items: [{
text: 'Detail #1',
leaf: true
}, {
text: 'Detail #2',
leaf: true
}]
}]
}
});
this.detailContainer = Ext.create("DetailContainer2", {});
this.nestedList = Ext.create('Ext.NestedList', {
docked: 'left',
width: 300,
flex: 1,
store: this.treeStore,
detailCard: true,
detailContainer: this.detailContainer,
listeners: {
scope: this,
leafitemtap: this.onLeafItemTap
}
});
this.setItems([this.detailContainer, this.nestedList]);
},
onLeafItemTap: function(nestedList, list, index, node, record, e) {
var detailCard = nestedList.getDetailCard();
// nestedList.setDetailContainer(Ext.create("DetailContainer1", {}))
// detailCard.setHtml(record.get('text') + "...");
}
});
Ext.application({
launch: function() {
var container = Ext.create("Ext.Container", {
layout: {
type: 'hbox'
},
items: [{
xtype: 'mainview'
}]
});
Ext.Viewport.add(container);
}
});
Finally I've figured out the proper way to solve your problem.
Some explanations:
If you want to display "customized detailContainer" at the right side of your hbox, it's quite obvious that you should disable detailCard config for your Ext.NestedList because it's designed to display inline (i.e. take place of your Ext.NestedList).
When using Ext.define, everything should be included in config (except extend, xtype, alias, id, so in this case, layout has to be put in config).
flex should be defined in config as well.
At leafitemtap event, simply update your detailContainer config and remove/add it again as it would not be updated dynamically.
Please take a look at modified code snippet below, it works perfectly for me.
Ext.Loader.setConfig({ enabled: true });
Ext.define('DetailContainer1', {
extend: 'Ext.Container',
xtype: 'detail-container1',
config: {
flex: 1,
layout: {
type: 'vbox'
},
style: 'background-color: #a9a9a9;',
items: [
{html: 'Detail Container1'}
]
}
});
Ext.define('DetailContainer2', {
extend: 'Ext.Container',
xtype: 'detail-container2',
config: {
flex: 1,
layout: {
type: 'vbox'
},
style: 'background-color: #c9c9c9;',
items: [
{html: 'Detail Container2', flex: 1},
{xtype: 'button', text: 'Hit me!', flex: 1}
]
}
});
Ext.define('ListItem', {
extend: 'Ext.data.Model',
config: {
fields: ['text']
}
});
Ext.define('Sencha.view.MainView', {
extend: 'Ext.Container',
xtype: 'mainview',
id: 'mainview',
config: {
layout: {
type: 'hbox'
},
},
initialize: function() {
this.treeStore = Ext.create('Ext.data.TreeStore', {
model: 'ListItem',
defaultRootProperty: 'items',
root: {
items: [
{
text: 'Containers',
items: [
{ text: 'Detail #1', leaf: true },
{ text: 'Detail #2', leaf: true }
]
}
]
}
});
this.detailContainer = Ext.create("DetailContainer2");
this.nestedList = Ext.create('Ext.NestedList', {
flex: 1,
store: this.treeStore,
listeners: {
scope: this,
leafitemtap: this.onLeafItemTap
}
});
this.setItems([this.nestedList, this.detailContainer]);
},
onLeafItemTap: function(nestedList, list, index, node, record, e) {
mainview = Ext.getCmp('mainview');
if (index==0) {
mainview.detailContainer = Ext.create("DetailContainer1");
} else {
mainview.detailContainer = Ext.create("DetailContainer2");
}
mainview.removeAt(1);
mainview.add(mainview.detailContainer);
}
});
Ext.application({
launch: function() {
Ext.Viewport.add(
Ext.create("Ext.Container", {
layout: {
type: 'card'
},
items: [
{ xtype : 'mainview'}
]
}));
}
});

Vbox container doesn't resize after add function. Extjs4

I'm trying to dynamically add a component to a container with the hbox layout specified
and have that container re-size itself to accommodate the new component. Currently the
new component is added, but the new and old components are re-sized / or tiled in the
container and the container maintains its size.
Here is a demonstration of the issue I'm having on jsfiddle.
Here is the relevant extjs4 javascript for the demo:
Ext.onReady(function(){
Ext.create ('Ext.panel.Panel', {
title: 'test',
width: 300,
height: 300,
items: [
{
xtype: 'container',
layout: 'hbox',
padding : 5,
items: [
{
xtype: 'container',
id: 'textfieldgroup',
flex: 1,
height: '100%',
border: false,
layout: {
type: 'vbox',
},
defaults: {
flex: 1,
},
items: [
{
xtype: 'textfield',
emptyText: 'type here',
},
],
},
{
xtype: 'button',
flex: .1,
text: '+',
listeners: {
'click' : function () {
var textFieldGroup =
Ext.ComponentQuery.query ('#textfieldgroup')[0];
var newTextField = Ext.widget ('textfield');
textFieldGroup.add (newTextField);
},
}
}
]
}
],
renderTo: Ext.getBody ()
});
});
I have found a suitable solution and my reasoning is that you cannot have a vbox dynamically expand within an hbox container. The added benefit is that this method enables you get rid of one level of nesting. Also using the layout property autoSize: true enables the vbox container to expand and dynamically re-size itself.
Ext.onReady(function() {
Ext.create('Ext.panel.Panel', {
title: 'test',
width: 300,
height: 300,
layout: 'vbox',
items: [
{
xtype: 'fieldset',
flex: 1,
title: 'Group of fields',
width: '100%',
items: [
{
xtype: 'container',
layout: 'hbox',
width: '100%',
items: [
{
flex: 1,
xtype: 'label',
text: 'Fields',
},
{
xtype: 'button',
flex: 1,
text: '+',
listeners: {
'click': function() {
var textFieldGroup =
Ext.ComponentQuery.query('#textfieldgroup')[0];
var newTextField = Ext.widget('textfield');
textFieldGroup.add(newTextField);
},
}}
]
},
{
xtype: 'container',
layout: {
type: 'vbox',
autoSize: true,
},
id: 'textfieldgroup',
defaults : {
// flex: 1,
},
items : [
{
xtype: 'textfield',
emptyText: 'type here',
}
]
}
]}
],
renderTo: Ext.getBody()
});
});​
​

Ext.getCmp("myForm") is undefined issue

In one of my panels i have a form panel
xtype: 'form',
id: 'formJobSummary',
layout: {
align: 'stretch',
type: 'hbox'
}
I wish to bind data to this and have the following code.
var form = Ext.getCmp('formJobSummary').getForm();
form.loadRecord(user);
I am getting:
Ext.getCmp("formJobSummary") is undefined
So obviously the loadRecord is out of scope. Given that my architecture is from the designer and has 2 files. Where do i put this loadRecord statement.
MyPanel.js
//Define a model with field names mapping to the form field name
Ext.define('UserModel', {
extend: 'Ext.data.Model',
fields: ['quotedPrice', 'name']
});
//Create an instance of the model with the specific value
var user = Ext.create('UserModel', {
quotedPrice: 'test',
name: 'test'
});
Ext.define('MyApp.view.MyPanel', {
extend: 'MyApp.view.ui.MyPanel',
initComponent: function () {
var me = this;
me.callParent(arguments);
me.down('button[text=Submit]').on('click',
me.onSubmitBtnClick, me);
me.down('button[text=Cancel]').on('click',
me.onCancelBtnClick, me);
},
onSubmitBtnClick: function () {
var conn = new Ext.data.Connection();
var est = Ext.getCmp('estimate');
alert(est.getValue());
conn.request({
method: 'POST',
url: 'tmp.php',
params: {
foo: "bar"
},
success: function (responseObject) { alert(responseObject.responseText); },
failure: function () { alert(est); }
});
},
onCancelBtnClick: function () {
}
});
var form = Ext.getCmp('formJobSummary').getForm(); //returns form1
form.loadRecord(user);
ui/MyPanel.js
Ext.define('MyApp.view.ui.MyPanel', {
extend: 'Ext.panel.Panel',
height: 600,
width: 950,
layout: {
align: 'stretch',
type: 'vbox'
},
title: 'JobPanel',
initComponent: function() {
var me = this;
Ext.applyIf(me, {
items: [
{
xtype: 'tabpanel',
activeTab: 0,
flex: 1,
items: [
{
xtype: 'panel',
layout: {
align: 'stretch',
type: 'hbox'
},
title: 'Job Summary',
items: [
{
xtype: 'form',
id: 'formJobSummary',
layout: {
align: 'stretch',
type: 'hbox'
},
bodyPadding: 10,
title: '',
url: '/submit.html',
flex: 1,
dockedItems: [
{
xtype: 'toolbar',
flex: 1,
dock: 'bottom',
items: [
{
xtype: 'button',
text: 'Submit'
},
{
xtype: 'button',
text: 'Cancel'
}
]
}
],
items: [
{
xtype: 'panel',
flex: 1,
items: [
{
xtype: 'radiogroup',
width: 400,
fieldLabel: 'Job Type',
items: [
{
xtype: 'radiofield',
boxLabel: 'Fix Price'
},
{
xtype: 'radiofield',
boxLabel: 'Production'
}
]
},
{
xtype: 'textfield',
id: 'quotedPrice',
name: 'quotedPrice',
fieldLabel: 'Quoted Price'
},
{
xtype: 'textfield',
id: 'clientPO',
name: 'clientPO',
fieldLabel: 'Client PO'
},
{
xtype: 'textfield',
id: 'jobQuantity',
name: 'jobQuantity',
fieldLabel: 'Job Quatity'
},
{
xtype: 'textfield',
id: 'filesOver',
name: 'filesOver',
fieldLabel: 'Files Over'
},
{
xtype: 'textfield',
id: 'previousJobId',
name: 'previousJobId',
fieldLabel: 'Previous JobId'
},
{
xtype: 'textfield',
id: 'estimate',
name: 'estimate',
fieldLabel: 'Estimate'
}
]
},
{
xtype: 'panel',
flex: 1
},
{
xtype: 'panel',
layout: {
align: 'stretch',
type: 'hbox'
},
flex: 1
}
]
}
]
},
{
xtype: 'panel',
title: 'Parts'
},
{
xtype: 'panel',
title: 'Process'
},
{
xtype: 'panel',
title: 'Invoice'
}
]
},
{
xtype: 'panel',
layout: {
align: 'stretch',
type: 'vbox'
},
title: 'FooterPanel',
flex: 1
}
]
});
me.callParent(arguments);
}
});
During the execution of your statement var form = Ext.getCmp('formJobSummary').getForm(); obviously the formJobSummary is undefined (ie, it doesn't exist!!). Your Ext.define doesn't create a instance of the view. The code you are trying to execute is on a global scope.. meaning it will get executed as soon as the javascript file is loaded. Ideally, It should get called after an instance of the class is created.
So, your solution will be identify, when you need to load your form with the data you have. For example, you might want to load the data when you render the form or when some button is clicked etc. That should help you solve the problem.