ExtJS pass variable from controller to view - extjs

I have a base controller that has a user object that is bind with the user data after login success. At this point the view that I need to pass the user object is already instantiated. Here is my base controller:
Ext.define('App.abstract.app.Controller', {
extend: 'Ext.app.Controller',
user: {} // user object that can be accessed by any controller
});
Now, here is my Main.js controller that extends from my base controller and that implements the login function:
Ext.define('App.controller.Main', {
extend: 'App.abstract.app.Controller',
refs: [{
ref: 'main',
selector: 'app-main'
},
{
ref: 'header',
selector: 'appheader'
}],
init: function (application) {
this.control({
"loginform": {
loguser: this.onLoginUser
},
'app-main #logoutBtn': {
//click: this.onLogoutUser
}
});
},
onLoginUser: function (form, values) {
var me = this;
values.password = App.util.MD5.encode(values.password);
App.util.Ajax.request({
url: '/api/Security/ValidateUser',
jsonData: values,
success: function (response, opts) {
var obj = Ext.decode(response.responseText);
if (obj.success) {
// sets the base controller user object so it can be available in all app
Ext.apply(me.user, obj.data);
me.getMain().layout.setActiveItem(1);
// HERE I WANT TO PASS MY me.user TO MY MAIN.js VIEW
}
}
});
},
}
});
Here is my Main.js view where I need to get the user object to I can set the User name in my footer (south panel).
Ext.define('App.view.Main', {
extend: 'Ext.container.Container',
requires: [
'Ext.layout.container.Card',
'Ext.layout.container.Border',
'Ext.layout.container.Accordion',
'Ext.form.Label',
'App.view.login.LoginForm',
'App.view.Dashboard',
'App.view.MainPanel',
'App.view.Header',
'App.view.menu.Accordion'
],
xtype: 'app-main',
layout: {
type: 'card'
},
items: [{
xtype: 'container',
layout: {
type: 'hbox',
align: 'stretch'
},
defaults: { flex: 1 },
items: [{
xtype: 'component'
}, {
xtype: 'container',
layout: {
type: 'vbox',
align: 'stretch',
},
defaults: { flex: 1 },
items: [{ xtype: 'component' }, { xtype: 'loginform' }, { xtype: 'component' }]
}, {
xtype: 'component'
}]
},
{
xtype: 'panel',
layout: {
type: 'border'
},
items: [
{
xtype: 'mainmenu',
width: 250,
region: 'west'
},
{
xtype: 'appheader',
region: 'north'
},
{
xtype: 'mainpanel',
region: 'center'
},
{
xtype: 'container',
region: 'south',
items:
[
{
xtype: 'toolbar',
margin: '0',
style: 'background-color:#3892D3;',
items: [
{
xtype: 'displayfield',
id: 'username',
fieldLabel: 'User Name',
width: 100,
labelWidth: 70,
style: 'color:#FFFFFF;',
value: '<span style="color:#FFFFFF;font-size:12px">HERE I NEED THE USER NAME</span>'
}
]
}
]
}
]
}
]
});
Any clue?

Related

Add new tab on tabPanel extJS

I'm just starting to learn extJS, so don't be very strict for my questions).
So this is my Main.js:
Ext.define('MyApp.view.main.Main', {
extend: 'Ext.tab.Panel',
xtype: 'app-main',
requires: [
'Ext.plugin.Viewport',
'Ext.window.MessageBox',
'MyApp.view.main.MainController',
'MyApp.view.main.MainModel',
'MyApp.view.main.List',
],
controller: 'main',
viewModel: 'main',
plugins: 'viewport',
ui: 'navigation',
header: {
layout: {
align: 'stretchmax',
},
title: {
bind: {
text: 'Some items',
},
flex: 0,
},
items: [
{
xtype: 'button',
text: 'Items',
margin: '10',
handler: 'onMouseDown', // this is my handler for create new tab
},
{
xtype: 'button',
text: 'Logout',
margin: '10',
handler: 'onClickButton',
},
],
},
tabBar: {
flex: 1,
layout: {
align: 'stretch',
overflowHandler: 'none',
},
},
defaults: {
bodyPadding: 20,
tabConfig: {
responsiveConfig: {
wide: {
iconAlign: 'left',
textAlign: 'left',
},
tall: {
iconAlign: 'top',
textAlign: 'center',
width: 120,
},
},
},
},
items: [
{
title: 'Items',
items: [
{
xtype: 'mainlist',
},
],
},
],
});
And this is my MainController.js:
Ext.define('MyApp.view.main.MainController', {
extend: 'Ext.app.ViewController',
alias: 'controller.main',
onConfirm: function (choice) {
if (choice === 'yes') {
//
}
},
onClickButton: function () {
localStorage.removeItem('MyAppLoggedIn');
this.getView().destroy();
Ext.create({
xtype: 'login',
});
},
onMouseDown: function () {
var tabPanel = Ext.create('Ext.TabPanel', {
listeners: {
tabchange: function (newTab) {
if (newTab) {
onAddTab(tabPanel);
}
},
},
items: [
{
title: 'Items',
items: [
{
xtype: 'mainlist',
},
],
},
],
renderTo: Ext.getBody(), // I want to render to tabPanel, not to body
});
},
});
After click I need to create a new Tab in my tabPanel.
May be I need just to change renderTo, but T'm sure that an elegant way exists:
renderTo: Ext.select('some tab class')
To add Tab to this Tab.Panel you need to use add function on the view.
To get View in ViewController you can use this.getView() function.
Example on Sencha (dynamically add tabs): https://examples.sencha.com/extjs/7.2.0/examples/kitchensink/?classic#reorderable-tabs
Example Your ViewController:
Ext.define('MyApp.view.main.MainController', {
extend: 'Ext.app.ViewController',
alias: 'controller.main',
onConfirm: function (choice) {
if (choice === 'yes') {
//
}
},
onClickButton: function () {
localStorage.removeItem('MyAppLoggedIn');
this.getView().destroy();
Ext.create({
xtype: 'login',
});
},
onMouseDown: function () {
var tabPanel = this.getView();
tabPanel.add({
items: [{
html: "xyz"
}]
});
}
});

extjs grid column renderer not calling

cannot find answer (
Ext.define('Cabinet.view.main.Main', {
extend: 'Ext.Container',
...
items: [
{
xtype: 'toolbar',
docked: 'top',
title: 'Sliding Menu',
items: [
{
xtype: 'button',
id: 'listButton',
iconCls: 'x-fa fa-bars',//list //x-fa fa-mortar-board
ui: 'plain',
listeners : {
tap: 'OnToggle'
}
}
]
},
{
xtype: 'container',
flex: 1,
itemId: 'contentPanel',
reference: 'mainCardPanel',
scrollable: 'y',
layoutConfig:{layoutOnCardChange:true},
layout: {
type: 'card',
anchor: '100%'
}
}
],
,
initialize: function(){
Ext.Viewport.setMenu(this.createMenu(),{
side: 'left',
reveal: true
});
this.callParent();
},
createMenu: function(){
var menu = Ext.create('Ext.Menu', {
width: 250,
defaultListenerScope: true,
scrollable: 'vertical',
controller: 'main',
ViewModel :
{
},
items: [
{
xtype: 'button',
text: 'asd',
viewName: 'acadPerf',
controller: 'main',
listeners : {
tap: function ()
{
if (Ext.Viewport.down('#contentPanel').getActiveItem() != 0) {
Ext.Viewport.down('#contentPanel').getActiveItem().destroy();
}
newView = Ext.create({xtype: this.viewName});
Ext.suspendLayouts();
Ext.Viewport.down('#contentPanel').setActiveItem(newView);
Ext.resumeLayouts(true);
load store
Ext.define('Cabinet.view.charts.acadPerf', {
extend: 'Ext.container.Container',
xtype: 'acadPerf',
viewModel: {
// type: 'charts'
},
layout: 'fit', //responsivecolumn
defaults: {
defaults: {
animation : !Ext.isIE9m && Ext.os.is.Desktop
}
},
listeners: {
render: function () {
this.down('grid').getStore().reload();
}
},
items: [
{
xtype: 'panel',
cls: 'faq-left-sidebar shadow',
header: false,
ui: 'light',
layout: 'fit',
responsiveConfig: {
'width < 1000': {
width: 0,
visible: false
},
'width >= 1000 && width < 1600': {
width: 230,
visible: true
},
'width >= 1600': {
width: 300,
visible: true
}
},
items: [
{
xtype:'grid',
reference: 'mainCardPanel',
store: 'acadPerfStore',
hideHeaders: true,
columns: [
{
text: 'asd',
xtype: 'gridcolumn',
flex: 1,
dataIndex: 'CONTROL_FORM',
cell: {
encodeHtml: false
},
renderer: 'renderTitleColumnAcademPerf'
},
]
}
]
}
],
...
when i at first time click to show menu and then tap on first button - all ok.
renderer work. I see all records.
but when i click once again to show menu and click on first button - i see only content of one record from store. but the size of verical scroll of grid is like all records rendered. in html i see only one record.
What's wrong?

Sencha touch 2: Set button handler parameter to lunch javascript function

I am using the sencha touch o'really example. Is a conference that have sessions and speakers associated. Imagine that I have a session list that when is tapped the view change to the session details.
I have a view like this:
Ext.define('MyApp.view.session.Detail', {
extend: 'Ext.Container',
xtype: 'session',
config: {
layout: 'vbox',
title: '',
items: [
{
flex: 1,
layout: 'fit',
scrollable: 'vertical',
xtype: 'sessionInfo'
},
{
flex: 2,
xtype: 'speakers',
store: 'SessionSpeakers',
items: [
{
xtype: 'listitemheader',
cls: 'dark',
html: 'Speakers'
}
]
},
{
flex: 1,
xtype: 'button',
text: "Decline Button" ,
handler: function () {
/*HERE I WANT TO PUT SOME DATA FROM THE SESSION OBJECT. FOR EXAMPLE */
alert({title});
alert({start_date});
alert({location});
}
}
]
}
});
So, I want to replace the {title} {start_date} {location} for the corresponding values of the current session.
By the way in the controller I have this:
onSessionTap: function(list, idx, el, record) {
var speakerStore = Ext.getStore('SessionSpeakers'),
speakerIds = record.get('speakerIds');
speakerStore.clearFilter();
speakerStore.filterBy(function(speaker) {
return Ext.Array.contains(speakerIds, speaker.get('id'));
});
if (!this.session) {
this.session = Ext.widget('session');
}
this.session.setTitle(record.get('title'));
this.getSessionContainer().push(this.session);
this.getSessionInfo().setRecord(record);
},
Thanks!!
I could resolve it.
I had to create the button on runtime.
:
In Session view:
Ext.define('MyApp.view.session.Detail', {
extend: 'Ext.Container',
xtype: 'session',
config: {
layout: 'vbox',
title: '',
items: [
{
flex: 1,
layout: 'fit',
scrollable: 'vertical',
xtype: 'sessionInfo'
},
{
flex: 2,
xtype: 'speakers',
store: 'SessionSpeakers',
items: [
{
xtype: 'listitemheader',
cls: 'dark',
html: 'Speakers'
}
]
}
]
}
});
In Session controller:
onSessionTap: function(list, idx, el, record) {
var speakerStore = Ext.getStore('SessionSpeakers'),
speakerIds = record.get('speakerIds');
speakerStore.clearFilter();
speakerStore.filterBy(function(speaker) {
return Ext.Array.contains(speakerIds, speaker.get('id'));
});
if (!this.session) {
this.session = Ext.widget('session');
}
else{
//To avoid multiple buttons
this.session.removeAt(2);
}
this.session.setTitle(record.get('title'));
this.session.add(
Ext.Button.create({
flex: 1,
xtype: 'button',
text:'My button' ,
handler: function () {
alert(record.get('title'));
alert(record.get('start_date'));
} })
);
this.getSessionContainer().push(this.session);
this.getSessionInfo().setRecord(record);
},

Adding carousel to panel

First program logic:
I have a main panel and there is a list at the left side and another panel at the right side.
When user touches the list item some html appears in right panel. What i need to do is using carousel instead of right panel.
My view
Ext.define('MyApp.view.MyPanel', {
extend: 'Ext.Panel',
xtype:'mypanel',
config: {
ui: 'dark',
layout: {
type: 'card'
},
items: [
{
xtype: 'titlebar',
docked: 'top',
title: 'Lezzet Dunyasi',
},
{
xtype: 'list',
docked: 'left',
id: 'mylist',
ui: 'round',
pinHeaders: false,
grouped: true,
//disableSelection: true,
width: 331,
itemTpl: [
'<img src="{img_url}" width="60" heigh="60"></img><span>{label}</span>'
],
store: 'Menius',
items: [
{
xtype: 'searchfield',
docked: 'top',
placeHolder: 'Search...',
},
]
},
{
xtype: 'panel',
styleHtmlContent:true,
style: {
backgroundImage: 'url(resources/img/Landscape.png)',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center'
},
id:'mypanel'
}
]
}
});
As you can see there is a xtype:panel and i tried to modify that code and i did it like this
xtype: 'carousel',
defaults{
styleHtmlContent:true,
id:'mypanel'},
items: [
{
html : 'Item 1',
style: 'background-color: #5E99CC'
},
{
html : 'Item 2',
style: 'background-color: #759E60'
},
{
html : 'Item 3'
}
]
Also i use a controller
Ext.define('MyApp.controller.MeniuController',{
extend:'Ext.app.Controller',
config:{
refs:{
leftMeniu:'mypanel list[id=mylist]',
myPanel:'mypanel panel[id=mypanel]'
},
control:{
leftMeniu:{
itemtap:'onItemTap'
}
}
},
onItemTap:function(list, index, item, record, e , opts)
{
var content = '<h2>' + record.get('label') +'</h2>' + record.get('html');
this.getMyPanel().setHtml( content );
}
});
And i modified this part like this
refs:{
leftMeniu:'mypanel list[id=mylist]',
myPanel:'mypanel carousel[id=mypanel]'
Although these modifications i can't run my code , what should i do ?
A couple of small issues that I see. You are missing a colon on defaults: And I think you want to move that id to one of your carousel elements, right? With your code, I'm only getting one page with the id defined at that level. If you move it down you will see three pages.
defaults: {
styleHtmlContent:true,
id:'mypanel' // IN WRONG PLACE?
},
[UPDATE]
I got it working so that you can write to any of your carousel panels. I just created a direct reference to each id in the refs:{} section. I'm drawing to the second page so drag it into view to see the updates.
Also, I'm adding the model, store, and app.js so that anyone reading this will have a complete working example.
Ext.define('MyApp.controller.MeniuController', {
extend:'Ext.app.Controller',
config:{
refs:{
leftMeniu:'mypanel list[id=mylist]',
// myPanel:'mypanel panel[id=mypanel]'
// myPanel:'mypanel carousel[id=mypanel]'
myFirstPanel:'#mypanel1',
mySecondPanel:'#mypanel2'
},
control:{
leftMeniu:{
itemtap:'onItemTap'
}
}
},
onItemTap:function(list, index, item, record, e, opts) {
var content = '<h2>' + record.get('label') + '</h2>' + record.get('html');
this.getMySecondPanel().setHtml(content);
this.getMyFirstPanel().setHtml(content);
}
});
Complete MyPanel View:
Ext.define('MyApp.view.MyPanel', {
extend: 'Ext.Panel',
xtype:'mypanel',
config: {
ui: 'dark',
layout: {
type: 'card'
},
items: [
{
xtype: 'titlebar',
docked: 'top',
title: 'Lezzet Dunyasi'
},
{
xtype: 'list',
docked: 'left',
id: 'mylist',
ui: 'round',
pinHeaders: false,
// grouped: true,
//disableSelection: true,
width: 331,
itemTpl: [
'{label}'
],
store: 'Menius',
items: [
{
xtype: 'searchfield',
docked: 'top',
placeHolder: 'Search...'
}
]
},
// {
// xtype: 'panel',
// styleHtmlContent:true,
// style: {
// backgroundImage: 'url(../images/risk2.png)',
// backgroundRepeat: 'no-repeat',
// backgroundPosition: 'center'
// },
// id:'mypanel'
// }
{
xtype: 'carousel',
defaults: {
styleHtmlContent:true
},
items: [
{
html: 'Item 1',
style: 'background-color: #5E99CC',
id:'mypanel1'
},
{
html: 'Item 2',
style: 'background-color: #759E60',
id:'mypanel2'
},
{
html: 'Item 3'
}
]
}
]
}
});
app.js
Ext.application({
name: "MyApp",
views: ['MyPanel'],
models: ['Meniu'],
stores: ['Menius'],
controllers: ['MeniuController'],
launch: function() {
Ext.Viewport.add(Ext.create('MyApp.view.MyPanel'));
}
});
Model:
Ext.define('MyApp.model.Meniu', {
extend: 'Ext.data.Model',
config: {
fields: ['img_url', 'label', 'html']
}
});
Store:
Ext.define('MyApp.store.Menius', {
extend: 'Ext.data.TreeStore',
config: {
model: 'MyApp.model.Meniu',
defaultRootProperty: 'items',
grouper: function(record) {
return record.get('label')[0];
},
root: {
text: 'foo',
items: [
{img_url: 'foo.png', label: 'one', html:'nice', leaf: true},
{img_url: 'foo.png', label: 'two', html:'carousels', leaf: true}
]
}
}
});

Sencha Touch 2: Trying to add a list to a panel

I have a panel which consists of a toolbar and tabs. On each tab there should be a list and a button. I believe I have this set up correctly except for the list, which I am trying to add as follows:
Ext.define('Myapp.view.Search', {
xtype: 'search',
extend: 'Ext.tab.Panel',
config: {
activeItem: 0,
tabBar: {
docked: 'top',
autoScroll: 'auto',
ui: 'light',
layout: {
pack: 'center'
}
},
items: [
{
xtype:'toolbar',
docked:'top',
ui: 'light',
title: 'Search'
},
{
title: 'Tab 1',
xtype: 'formpanel',
items: [
{
xtype: 'Mylist' //DOES NOT WORK
},
{
xtype: 'panel',
defaults: {
xtype: 'button',
style: 'margin: 0.1em',
flex : 1
},
layout: {
type: 'hbox'
},
items: [
{
text: 'Button 1',
}
]
}
]
},
{
title: 'Tab 2',
xtype: 'formpanel',
items: [
{
xtype: 'panel',
defaults: {
xtype: 'button',
style: 'margin: 0.1em',
flex : 1
},
layout: {
type: 'hbox'
},
items: [
{
text: 'Button 1',
}
]
}
]
}
]
}
});
Please let me know what I am doing wrong and thanks for you help!
EDIT: added Mylist below:
Ext.define('Myapp.view.Mylist', {
extend: 'Ext.dataview.NestedList',
xtype: 'Mylist',
config: {
store: 'Sections'
},
getTitleTextTpl: function() {
return '{name}';
},
getItemTextTpl: function(node) {
return '<strong>{name}</strong>';
},
});
Try these
in View:
{
xtype: 'panel',
flex: 4,
width: '400px',
height:"700px",
layout: {
type: 'fit'
},
items: [
{
xtype: 'list',
// loadingText:"Loading Category",
styleHtmlContent: true,
// id:"mylist2",
width:"300px",
itemTpl:
'<div class="mycon">'+
'<input type="image" id="click" img src="{coupon_image}" style="max-width:130%;border:6px double #000000;" width="200" height="200"' +'style="padding:3px;">' +
'</div>'+
'<div><font size="2" color="red"><b>Coupon Name:</b></font></div>'+
'<div><font size="2" color="green"><b>{coupon_name}</b></font></div>'+
'</div>',
store : 'ViewCategoryStore',
},
]
}
in controller:
Ext.define('Expressdeal.controller.ViewCategoryController', {
extend: 'Ext.app.Controller',
config: {
refs: {
viewcat : 'viewcategory' // xtype of the view
},
control: {
'viewcategory list: {
activate: 'onActivate',
itemtap: 'onItemTap',
},
Where you define your list in your List view you should say
alias:'widget.Mylist'
instead of
xtype:'Mylist'
also your list needs an itemTpl config im pretty sure.
config: {
store: 'Sections',
itemTpl:'{example}'
}
And as long as the store that drives your list is in working shape the list should appear correctly

Resources