PDA

View Full Version : Viewport with template in header



Spenna
1 Nov 2011, 12:53 AM
Hi.

I'm new to ExtJS 4, and are wondering whats the correct way to bind data to a 'header' using the MVC pattern.
Basically i have a Viewport defined in app.js


Ext.create('Ext.container.Viewport', {
layout: 'border',
items: [{
xtype: 'header'
},{
xtype: 'dashboard'
}]
});

And the app/view/Header.js looks like this:


Ext.define('SF6.view.Header', {
extend: 'Ext.Component',
region: 'north',
alias : 'widget.header',
tpl: Ext.create('Ext.Template', '<div class="x-panel-header" style="border-bottom: 1px solid #CCC;background-color:#FFF">'
+ '<h1 style="float:left;">Application Title</h1>'
+ '<tpl if="authenticated">'
+ '<span style="float:right; font-size:11px;">Logged in as <b>{username}</b> ({role}) | {company} | <a href="#">Log out</a></span><br>'
+ '</tpl>'
+'</div>'),
initComponent: function() {
this.callParent(arguments);
}


When user is not authenticated i bring up a modal login-window which is controlled by my users controller.

28975
http://screencast.com/t/qnwUjDdh

My question is what is the correct way to store current user in the 'Application' and how to apply the current users value to the header?

mitchellsimoens
1 Nov 2011, 6:31 AM
My question is what is the correct way to store current user in the 'Application' and how to apply the current users value to the header?

This is really up to you... my favorite way is to save it on the app namespace.


app.user = {...};

Spenna
1 Nov 2011, 6:39 AM
Cool, this is what i've ended up doing. How about the view in header? Whats the 'ext4' way of updating a part of the view when user is logged in?

mitchellsimoens
1 Nov 2011, 6:47 AM
I assign the tpl to a container and then use the update method and pass in an Object which is the user details.

Spenna
30 Nov 2011, 5:40 AM
I've got another question regarding this - once logged in, template is rendered ok - but how do i capture click on the 'log out' link? (Its part of the original template)

I would prefer to have definition of it in my 'Users' controller am unsure of how to achive it. Something like this:
Ext.define('SF6.controller.Users', {
extend: 'Ext.app.Controller',
init: function() {
this.control({
'header a': {
click: this.logout
}
})
},
......
logout: function () {
Ext.Ajax.request({
url: '/users/logout',
success: function (response) {
var view = Ext.widget('userlogin'); // brings up login window
}
});
}
);

mitchellsimoens
30 Nov 2011, 6:28 AM
ComponentQuery cannot listen for DOM elements.

You have to do something like this after a component has rendered:


cmp.mon(cmp.el, {
scope : this,
delegate : 'a',
click : function() {...}
});