问题
I am trying to change value of variable and i did as instruction in this post but value does not changes.
My codes are as follow:
Ext.define('MyApp.view.OnlineOffline', {
extend: 'Ext.Panel',
alias: "widget.onlineoffline",
config: {
onlineStatus: 0,
items: [
{
xtype: 'container',
layout: 'hbox',
cls: 'offline-wrap',
items:[
{
xtype: 'image',
cls: 'offlineCheck',
id:'onlineButton',
width: 85,
height:20,
listeners: {
tap: function (button)
{
var me = button.up('onlineoffline')
if (!Ext.device.Connection.isOnline())
{
Ext.Msg.alert('Please connect to <br/>working internet Connection?');
this.element.removeCls('onlineCheck');
this.element.addCls('offlineCheck');
me.setonlineStatus(1);
}
else {
if(me.getOnlineStatus())
{
console.log( 'connection yes if' + me.getOnlineStatus());
me.setOnlineStatus(1);
this.element.removeCls('onlineCheck');
this.element.addCls('offlineCheck');
}
else{
this.element.removeCls('offlineCheck');
this.element.addCls('onlineCheck');
me.setOnlineStatus(0);
console.log( 'connection yes else' + me.getOnlineStatus());
}
}
}
}
},
]
}]
}
});
回答1:
A couple things here...
First, you are initializing me
as a global variable, which is a bad idea. Rather than doing this, get a reference to what you have as me
using the button:
listeners: {
tap: function (button) {
var me = button.up('onlineoffline')
...
The problem you are having is caused because you're calling the wrong function. Your config
parameter is defined as onlineStatus
, but you are calling setonlinestatus()
. Call me.setOnlineStatus()
instead. The camel-casing for the generated getters and setters will be done exactly as your config
param, except the first letter will be capitalized.
来源:https://stackoverflow.com/questions/18464355/unable-to-change-the-value-of-variable-in-button-click-sencha