Ext JS StatusBar with clock - extjs4

I'm trying to create a status bar with clock.
I found example in sencha examples, but I'm trying to build it more object oriented way.
Ext.define('Urlopy.Components.Statusbar', {
extend : 'Ext.ux.statusbar.StatusBar',
id : 'basic-statusbar',
defaultText : 'Wczytane...',
defaultIconCls : 'x-status-valid',
iconCls : 'x-status-valid',
autoClear : 3000,
initComponent : function() {
this.currentUserDisplay = Ext.create('Ext.toolbar.TextItem');
this.currentUserDisplay.setText('Not logged in!');
this.timeDisplay = Ext.create('Ext.toolbar.TextItem');
this.timeDisplay.setText(Ext.Date.format(new Date(), 'Y-m-d H:i:s'));
Ext.apply(this, {
items : [this.currentUserDisplay, {
xtype : 'tbseparator'
}, this.timeDisplay],
listeners : {
render : {
fn : function() {
Ext.TaskManager.start({
run : function() {
this.timeDisplay.setText(Ext.Date.format(new Date(),'Y-m-d H:i:s'));
},
interval : 1000
});
},
delay : 100
}
}
});
this.callParent();
},
setCurrentUser : function(username) {
this.currentUserDisplay.setText('Logged as' + ": " + username);
},
startLoad : function(message) {
if (message !== null) {
this.showBusy({
text : message,
iconCls : "x-status-busy"
});
} else {
this.showBusy();
}
},
endLoad : function() {
this.clearStatus({
useDefaults : true
});
}
});
The first time component shows it displays date and time correct, but it doesn't update.
Problem is probably TaskManager or my listeners.
Instead of them I've tried doing this:
initComponent : function() {
this.currentUserDisplay = Ext.create("Ext.toolbar.TextItem");
this.currentUserDisplay.setText('Nie jesteś zalogowany!');
this.timeDisplay = Ext.create("Ext.toolbar.TextItem");
this.timeDisplay.setText(Ext.Date.format(new Date(), "Y-m-d H:i:s"));
Ext.apply(this, {
items : [this.currentUserDisplay, {
xtype : 'tbseparator'
}, this.timeDisplay]
});
this.callParent();
var task = {
run : function() {
this.timeDisplay.setText(Ext.Date.format(new Date(), "Y-m-d H:i:s"));
},
interval : 1000
}
Ext.TaskManager.start(task);
},
but no luck :(
I know that this is probably a tiny mistake, but can't find it.

What you have is a scope issue, this in your run function doesn't point to the status bar. you need to add a scope: this after the interval.
see example http://jsfiddle.net/nscrob/kR9ev/17/

Related

how to disable a DataTables/TableTools button

I'm using DataTable 1.10 and TableTools 2.2.1.
Given the following snipped I would like to disable/enable the edit-button.
var myTable = $("#myTable ").DataTable({
tableTools : {
"aButtons" : [ {
"sExtends" : "text",
"sButtonText" : "Edit",
"fnClick" : function(nButton, oConfig, oFlash) {
/* some stuff */
}
}]
}
})
Is there a possibility to do this at runtime?
Thanks a lot
This was a good question! Seems that the fnClick in
dataTable.tabletools().fnSettings().buttonSet[id].fnClick
only is a reference to the event stored elsewhere, not accessible (changing fnClick on the API has no effect). However, you can use the predefined class DTTT_disabled and check for that in your fnClick-handler :
var dataTable = $("#example").DataTable({
sDom: 'TC',
oTableTools : {
aButtons : [{
sExtends : "text",
sButtonText : "Edit",
fnClick : function(nButton, oConfig, oFlash) {
if ($(nButton).hasClass('DTTT_disabled')) return;
alert('edit button clicked');
}
}]
}
});
example with a checkbox enabling or disabling the button :
$("#enable").click(function() {
if ($(this).is(':checked')) {
$('.DTTT_button_text').removeClass('DTTT_disabled');
} else {
$('.DTTT_button_text').addClass('DTTT_disabled');
}
});
see demo -> http://jsfiddle.net/ev2N2/

Sencha touch 2 custom list item button tap not fired

I am developing a Hybrid Mobile App over sencha touch 2.
Now I was in a need of a custom component to be specific a custom list item consisting of a button along with.
My view has rendered as i wanted to but the button that is added to the list item is not firing the TAP event as expected. Instead on every tap, the ITEMTAP event is fired which is creating a bit of mess.
Can someone suggest me where to look to make this work ?
Below is the code for the custom component that i created:
var listView = {
xtype : "list",
id : "desk-list-search-results",
cls : "desk-list-search-results-cls",
selectedCls : "",
defaultType:"desksearchlistitem",
store : "deskstore",
flex : 2
};
This is the code for the custom component
Ext.define("MyApp.view.DeskSearchListItem",{
extend:"Ext.dataview.component.ListItem",
requires:["Ext.Button"],
alias:"widget.desksearchlistitem",
initialize:function()
{
this.callParent(arguments);
},
config:{
layout:{
type:"hbox",
align:"left"
},
cls:'x-list-item desk-search-list-item',
title:{
cls:"desk-list-item",
flex:0,
styleHtmlContent:true,
style:"align:left;"
},
image:{
cls:"circle_image",
width:"28px",
height:"28px"
},
button:{
cls:'x-button custom-button custom-font bookdesk-button',
flex:0,
text:"Book",
width:"113px",
height:"46px",
hidden:true
},
dataMap:{
getTitle:{
setHtml:'title'
}
}
},
applyButton:function(config)
{
return Ext.factory(config,Ext.Button,this.getButton());
},
updateButton:function(newButton,oldButton)
{
if(newButton)
{
this.add(newButton);
}
if(oldButton)
{
this.remove(oldButton);
}
},
applyTitle:function(config)
{
return Ext.factory(config,Ext.Component,this.getTitle());
},
updateTitle:function(newTitle,oldTitle)
{
if(newTitle)
{
this.add(newTitle);
}
if(oldTitle)
{
this.remove(oldTitle);
}
},
applyImage:function(config)
{
return Ext.factory(config,Ext.Component,this.getImage());
},
updateImage:function(newImage,oldImage)
{
if(newImage)
{
this.add(newImage);
}
if(oldImage)
{
this.remove(oldImage);
}
}
})
Finally got a solution to this,
It can be done into the view using the listeners object.
Here is the code sample for it.
listeners:{
initialize:function()
{
var dataview = this;
dataview.on("itemtap",function(list,index,target,record,event){
event.preventDefault();
});
dataview.on("itemswipe",function(list,index,target,record,event){
if(event.direction === "right")
{
var buttonId = target.element.down(".bookdesk-button").id;
var buttonEl = Ext.ComponentQuery.query("#"+buttonId)[0];
if(Ext.isObject(buttonEl))
{
buttonEl.setZIndex(9999);
buttonEl.show({
showAnimation:{
type:"slideIn",
duration:500
}
});
var listeners = {
tap:function(btn,e,opt)
{
console.log("Button Tapped");
}
};
buttonEl.setListeners(listeners);
}
else
{
console.log("This is not a valid element");
}
}
});
}
}
Thanks Anyways.

Failed to change the text of a titlebar in a container with sencha touch 2.1

I want to create an application with users login and logout. When the user logs in ,the system will store the user information such as username,sessionid into the localstorage and then go to the index page. The index page has a titlebar with username on the left and a button to log out.But it failed to get the data stored in the localstorage that was stored when a user logs in when the index page appears.
My code is as below:
index page:
Ext.define('MyApp.view.MyPanel', {
extend : 'Ext.Panel',
id : 'myPanel',
config : {
xtype : 'panel',
layout : 'vbox',
height : '100%',
width : '100%',
items : [
{
xtype : 'titlebar',
docked : 'top',
title : 'my panel',
items : [
{
align : 'left',
id : 'portal-username',
text : 'qq' //i used localStorage.getItem('userName') first,but not as good.
},
{
align : 'right',
text : 'log out',
listeners : {
tap : function(){
var userName=null;var rememberPassword=null;var password=null;
if(userName=Ext.getCmp('userName'))userName.setValue('');
if(password=Ext.getCmp('password'))password.setValue('');
if(rememberPassword=Ext.getCmp('rememberPassword'))rememberPassword.setChecked(false);
//localstorage
localStorage.removeItem('userName','');
localStorage.removeItem('password','');
localStorage.removeItem('SessionId','');
localStorage.removeItem('rememberPassword','');
var oldp=Ext.getCmp('loginPanel');if(oldp)oldp.destroy();
var newActivePanel = Ext.create('MyApp.view.LoginPanel');
Ext.Viewport.add(newActivePanel);
Ext.Viewport.animateActiveItem(newActivePanel,{type:'slide',direction:'left'});
}
}
}
]
}
]
}
});
part of login panel:
items : [
{
xtype : 'button',
text : 'login',
ui : 'action',
handler : function() {
var userName = Ext.getCmp('userName').getValue();
var password = Ext.getCmp('password').getValue();
var rememberPassword = Ext.getCmp('rememberPassword').getChecked()+ '';
if(!userName||!password){
Ext.Msg.alert('prompt','login failure,try again!',Ext.emptyFn);
}
else Ext.Ajax.request({
url : remoteServer,
params : {
loginName: userName,
passWord : password,
SessionId : localStorage.getItem('SessionId');
},
method : 'POST',
callback : function(options,success,response) {
if (success) {
var result = Ext.JSON.decode(response.responseText.trim());
var status = result.status;
if (status == 'success') {
if (rememberPassword == 'true')
localStorage.setItem('rememberPassword',rememberPassword);
localStorage.setItem('userName',userName);
localStorage.setItem('password',password);
localStorage.setItem('SessionId',result.SessionId);
var oldp=Ext.getCmp('MyPanel');if(oldp)oldp.destroy();
var newActivePanel = Ext.create('MyApp.view.MyPanel');
Ext.Viewport.add(newActivePanel);
Ext.Viewport.animateActiveItem(newActivePanel,{type:'slide',direction:'left'});
} else {
localStorage.setItem('userName','');
localStorage.setItem('password','');
localStorage.setItem('JSessionId','');
localStorage.setItem('rememberPassword','');
Ext.Msg.alert('prompt','login failure,try again!',Ext.emptyFn);
}
} else {
Ext.Msg.alert('prompt','login failure,try again!',Ext.emptyFn);
}
}
});
}
}
]
Below is my controller :
Ext.define('MyApp.controller.MyController',
{
extend : 'Ext.app.Controller',
id : 'MyController',
config : {
control : {
MyPanel : {
show : 'panelActive'
},
},
refs : {
MyPanel : '#myPanel'
}
}//config
,panelActive : function(){
Ext.getCmp('portal-username').setText(localStorage.getItem('userName'));
}
});
After Following line
Ext.Viewport.animateActiveItem(newActivePanel,{type:'slide',direction:'left'});
in login handler function just set the username using
Ext.getCmp('portal-username').setText(userName);

ExtJs3.4.0 to ExtJs4.1.1 upgrade issues

ExtJS4: I am having problems while upgrading my application ExtJs version from 3.4.0 to 4.1.1a.
My 3.4.0 version code:
this.jsonStore = new Ext.data.JsonStore({
proxy : new Ext.data.HttpProxy({
url: 'rs/environments',
disableCaching: true
}),
restful : true,
storeId : 'Environments',
idProperty: 'env',
fields : [
'ConnectionName', 'Type'
]
});
this.colmodel = new Ext.grid.ColumnModel({
defaults: {
align: 'center'
},
columns: [{
header: Accero.Locale.text.adminlogin.connectionsHeading,
width : 140,
dataIndex: 'ConnectionName'
},
{
header: Accero.Locale.text.adminlogin.connectionTypeHeader,
width : 120,
dataIndex: 'Type'
}]
});
config = Ext.apply({
enableHdMenu: false,
border : true,
stripeRows : true,
store : this.jsonStore,
view : new Ext.grid.GridView(),
header : false,
colModel : this.colmodel,
sm : new Ext.grid.RowSelectionModel({singleSelect: true}),
loadMask: {
msg: Accero.Locale.text.adminlogin.loadingmask
}
}, config);
I made below changes to make application work with ExtJs4.1.1:
var sm = new Ext.selection.CheckboxModel( {
listeners:{
selectionchange: function(selectionModel, selected, options){
// Must refresh the view after every selection
myGrid.getView().refresh();
// other code for this listener
}
}
});
var getSelectedSumFn = function(column){
return function(){
var records = myGrid.getSelectionModel().getSelection(),
result = 0;
Ext.each(records, function(record){
result += record.get(column) * 1;
});
return result;
};
}
var config = Ext.create('Ext.grid.Panel', {
autoScroll:true,
features: [{
ftype: 'summary'
}],
store: this.jsonStore,
defaults: { // defaults are applied to items, not the container
sortable:true
},
selModel: sm,
columns: [
{header: Accero.Locale.text.adminlogin.connectionsHeading, width: 140, dataIndex: 'ConnectionName'},
{header: Accero.Locale.text.adminlogin.connectionTypeHeader, width: 120, dataIndex: 'Type'}
],
loadMask: {
msg: Accero.Locale.text.adminlogin.loadingmask
},
viewConfig: {
stripeRows: true
}
}, config);
With these changes, I am getting the error at my local file 'ext-override.js' saying 'this.el is not defined'.
I debug the code and found that, in the current object this, there is no el object.
ext-override.js code:
(function() {
var originalInitValue = Ext.form.TextField.prototype.initValue;
Ext.override(Ext.form.TextField, {
initValue: function() {
originalInitValue.apply( this, arguments );
if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
this.el.dom.maxLength = this.maxLength *1;
}
}
}
);
})();
Kindly suggest where am I going wrong?
Thanks in advance...
Seriously, use more lazy initialization! Your code is a hell of objects, all unstructured.
First of all, you can override and use the overridden method more easily with something like that (since 4.1)
Ext.override('My.Override.for.TextField', {
override : 'Ext.form.TextField',
initValue: function() {
this.callOverridden(arguments);
if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
this.el.dom.maxLength = this.maxLength *1;
}
}
});
But: The method initValue is called in initField (and this in initComponent) so that you cannot have a reference to this.me because the component is actually not (fully) rendered.
So, this should help (not tested):
Ext.override('My.Override.for.TextField', {
override : 'Ext.form.TextField',
afterRender: function() {
this.callOverridden(arguments);
if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
this.el.dom.maxLength = this.maxLength *1;
}
}
});
But I'm strongly recommend not to use such things within overrides. Make dedicated components which will improve code readibility.

Sencha touch: cannot get reference to items in panel

I'm am trying to get MVC to work in my first application and am trying to change cards based button clicked in toolbar.
I'm getting a runtime error when I click the hello or world button in the toolbar: TypeError: Cannot read property 'items' of undefined on the line:
var exerciseListPanel = this.items.items[0], // CODE ERRORS HERE!
I'm having a problem in getting a reference to the items in the panel, WHY?
Here is the complete code for the panel:
MyApp.views.HomeScreenPanel = Ext.extend(Ext.Panel, {
layout : 'card',
cardSwitchAnimation: 'slide',
initComponent : function() {
this.dockedItems = this.buildDockedItems();
this.items = this.buildItemList();
MyApp.views.HomeScreenPanel.superclass.initComponent.call(this);
},
buildItemList : function(){
return [
new Ext.Panel({html:"hello"}),
new Ext.Panel({html:"world"}),
];
},
buildDockedItems : function(){
return [
this.buildTopDockToolbar(),
this.buildBottomDockToolbar(),
];
},
buildTopDockToolbar : function(){
return {
xtype : 'toolbar',
dock : 'top',
title : 'My first MVC app',
};
},
buildBottomDockToolbar : function(){
return this.NavigationToolbar();
},
NavigationToolbar : function(){
return{
xtype : 'toolbar',
dock : 'bottom',
defaults : {
handler : this.NavigationToolbarHandler,
controller: 'NavigationBarController'
},
items : [
{
text : 'hello',
action: 'hello'
},
{
text : 'world',
action : 'world'
}]
};
},
NavigationToolbarHandler : function(btn) {
var exerciseListPanel = this.items.items[0], // CODE ERRORS HERE!
workoutListPanel = this.items.items[1];
Ext.dispatch({
controller : btn.controller,
action : btn.action,
views : {
exerciseListPanel: exerciseListPanel,
workoutListPanel: workoutListPanel,
}
})
},
});
Ext.reg('HomeScreenPanel',MyApp.views.HomeScreenPanel);
Here is the code for the controller but I don't there is a problem here:
Ext.regController('NavigationBarController', {
world : function(dataObj) {
Ext.Msg.alert(
'world!',
'world button pressed from toolbar',
Ext.emptyFn
);
MyApp.views.HomeScreenPanel.setActiveItem(dataObj.views.worldPanel); // CORRECT SYNTAX????
},
hello : function(dataObj) {
Ext.Msg.alert(
'hello',
'hello button pressed',
Ext.emptyFn
);
MyApp.views.HomeScreenPanel.setActiveItem(dataObj.views.helloPanel); // CORRECT SYNTAX?????
}
});
It is probably just a scope issue. try:
exerciseListPanel = MyApp.views.HomeScreenPanel.items.items[0];
In this context this is probably the button.
Items is a MixedCollection so use the get function.
http://docs.sencha.com/touch/1-1/#!/api/Ext.util.MixedCollection
MyApp.views.HomeScreenPanel.items.get(0);
Bit late but for anyone else googling in the future ;)