【问题标题】:extJS - Call a function from another function within the same controllerextJS - 从同一控制器中的另一个函数调用一个函数
【发布时间】:2013-11-13 18:42:28
【问题描述】:

超级愚蠢的问题,但我无法让它工作,我们如何从同一个Controller 中的另一个函数调用一个函数?我使用煎茶建筑师。

这是我的控制器,我有一个监听器和一个函数,我想从监听器调用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
            }
        });
    }

});

当我调用 component.add(generateField(n.customField.name)); 时,我收到“找不到函数”错误

【问题讨论】:

    标签: javascript extjs4 extjs4.2 sencha-architect


    【解决方案1】:

    onLocalOrganisationInfoAfterRender: function(component, eOpts) {之后

    粘贴var that = this;

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

    【讨论】:

      【解决方案2】:

      另一种方法是设置 ajax 请求回调的范围。

      这样

      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
      
          });
      

      【讨论】:

      • 感谢您的解释。这也是一个正确的答案,但我会接受正弦的答案,因为他早些时候发布了它:-)
      猜你喜欢
      • 1970-01-01
      • 2015-02-06
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多