最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - extJS - Call a function from another function within the same controller - Stack Overflow

programmeradmin4浏览0评论

Super-stupid question, but I can't make it work, how do we call a function from another function within the same Controller? I use sencha architect.

Here is my controller where I a have a listener and a function, I want to call generateField function from the listener

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

    views: [
        'LocalOrgPanel'
    ],

    onLocalOrganisationInfoAfterRender: function(ponent, eOpts) {

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

        ponent.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) {
                    ponent.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 ponent.add(generateField(n.customField.name)); I get "function not found" error

Super-stupid question, but I can't make it work, how do we call a function from another function within the same Controller? I use sencha architect.

Here is my controller where I a have a listener and a function, I want to call generateField function from the listener

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

    views: [
        'LocalOrgPanel'
    ],

    onLocalOrganisationInfoAfterRender: function(ponent, eOpts) {

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

        ponent.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) {
                    ponent.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 ponent.add(generateField(n.customField.name)); I get "function not found" error

Share Improve this question asked Nov 13, 2013 at 18:42 OlegOleg 3,0148 gold badges46 silver badges73 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

After onLocalOrganisationInfoAfterRender: function(ponent, eOpts) {

paste var that = this;

And then ponent.add(that.generateField(n.customField.name));

An alternative way is to set scope for the ajax request call back.

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

    });
发布评论

评论列表(0)

  1. 暂无评论