Function 如何访问一个函数';Extjs4中另一个函数中的s变量?
我在extjs4中工作。我有一个带有复选框的视图。在控制器中,我编写了一个函数来获取这些复选框值Function 如何访问一个函数';Extjs4中另一个函数中的s变量?,function,extjs,extjs4,Function,Extjs,Extjs4,我在extjs4中工作。我有一个带有复选框的视图。在控制器中,我编写了一个函数来获取这些复选框值 chekfeature: function (cmp) { cmp.mon(cmp.getEl(), 'click', function (event, target) { getFeatureId = target.id; console.log(getFeatureId); return getFeatureId;
chekfeature: function (cmp) {
cmp.mon(cmp.getEl(), 'click', function (event, target) {
getFeatureId = target.id;
console.log(getFeatureId);
return getFeatureId;
}, this, {
delegate: 'input'
});
},
所以我在getFeatureId中获取复选框值。我想在同一控制器的另一个函数中访问此变量。我试过这个:
getFeature:function()
{
$getvalue = this.chekfeature();
}
但这不起作用。那么,如何访问另一个函数中的一个函数变量呢?首先,我认为您需要了解一些javascript基础知识……如果您了解了这些,请查看sencha的示例和教程 在视图中,您有复选框组件 在控制器中,您可以监听 如果控制器中有另一个需要该值的函数,则将其作为变量传递给需要该值的函数(=基本javascript),或者获取复选框组件并调用该方法 以下是一个例子:
您的
checkfeature
函数没有返回任何内容。实际上,您是从checkfeature
中的事件处理程序函数返回的
您必须在这里质疑您的执行流程:您知道只有在单击组件后才能知道getFeatureId
的值,因此不能随时调用getFeature
。您需要等待单击的发生,然后从那里继续
具体地说,您需要将要放入getFeature
的处理封装在回调中,该回调将在用户单击组件后执行
以下是如何做到这一点:
checkFeature: function(cmp, callback) {
cmp.mon(cmp.getEl(), 'click', function(event, target) {
// you most use var with your local variables and
// avoid messing up or depending on the global namespace
var featureId = target.id;
console.log(featureId);
// fires the callback
callback(featureId);
// do not return from the event handler because this result
// will be interpreted by the event manager (i.e. in many
// cases, returning false from an event handler will stop
// the processing that fired the event)
});
// see, there's no return here
}
以下是您将如何使用此方法:
getFeature: function() {
this.checkFeature(function(featureId) {
// featureId is available here
});
// unfortunately, you won't be able to return something
// depending on featureId from here... if you need to do so,
// you'll have to use another callback in this method.
}
为了简单起见,在本例中,我使用一个简单的调用执行回调。但是,在使用此模式时,通常最好为函数的执行范围提供支持
也就是说,您通常应该这样做(这个构造在Ext的代码以及其他lib中无处不在):
用法:
// let's say we are in the context of an object method
var that = this;
myAsyncFunction(function(result, result2) {
console.log(this === that); // true
}, this);
您可以尝试使用一个全局变量。先生,您能详细说明一下吗?也就是说,VDP是正确的,您试图实现的目标可能不应该这样做。您应该从事件处理程序函数开始处理,然后从那里同步继续。
function myAsyncFunction(callback, scope) {
// example results you want to pass
var result = 1,
result2 = 2;
// execute the callback in the given scope, or default
// to the current `this`
callback.call(scope || this, result, result2);
}
// let's say we are in the context of an object method
var that = this;
myAsyncFunction(function(result, result2) {
console.log(this === that); // true
}, this);