简体   繁体   English

extJS-从同一控制器中的另一个函数调用一个函数

[英]extJS - Call a function from another function within the same controller

Super-stupid question, but I can't make it work, how do we call a function from another function within the same Controller ? 超级愚蠢的问题,但我无法解决,我们如何从同一Controller另一个函数调用一个函数? I use sencha architect. 我使用sencha architect。

Here is my controller where I a have a listener and a function, I want to call generateField function from the listener 这是我的控制器,其中有一个侦听器和一个函数,我想从侦听器调用generateField函数

Ext.define('Medlemssystem.controller.MemberOrganisationController', {
    extend: 'Ext.app.Controller',

    views: [
        'LocalOrgPanel'
    ],

    onLocalOrganisationInfoAfterRender: function(component, eOpts) {

        main_id = component.up('#memberTab').main_id;

        component.removeAll();

        Ext.Ajax.request({
            url: 'OrganizationCustomFieldServlet',
            method: 'GET',
            dataType: 'json',
            params: {
                "operation" : "get",
                "org_id" : main_id 
            },
            success: function(response) {
                var result = Ext.decode(response.responseText);
                result.forEach(function(n) {
                    component.add(generateField(n.customField.name));
                });
            },
            failure: function() {
                console.log('woops');
            }
        });
    },

    generateField: function(name, type, id, required, description) {
        var field = Ext.create("Ext.form.field.Text", {fieldLabel:name});

        return field;
    },

    init: function(application) {
        this.control({
            "LocalOrgPanel": {
                afterrender: this.onLocalOrganisationInfoAfterRender
            }
        });
    }

});

when I call component.add(generateField(n.customField.name)); 当我调用component.add(generateField(n.customField.name)); I get "function not found" error 我收到“找不到功能”错误

After onLocalOrganisationInfoAfterRender: function(component, eOpts) { onLocalOrganisationInfoAfterRender: function(component, eOpts) {之后onLocalOrganisationInfoAfterRender: function(component, eOpts) {

paste var that = this; 粘贴var that = this;

And then component.add(that.generateField(n.customField.name)); 然后component.add(that.generateField(n.customField.name));

An alternative way is to set scope for the ajax request call back. 另一种方法是设置ajax请求回调的范围。

Like this 像这样

Ext.Ajax.request({
        url: 'OrganizationCustomFieldServlet',
        method: 'GET',
        dataType: 'json',
        params: {
            "operation" : "get",
            "org_id" : main_id 
        },
        success: function(response) {
           console.log(this); //<-- scope here is Window by default, unless scope is set below
        },
        failure: function() {
            console.log('woops');
        },
        scope :this //<--   Sets the controller as the scope for the success call back

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM