Issue with handling 'this' in submit function in Sencha touch/JavaScript not resolved

I am currently working on developing a form using Sencha Touch that is designed to allow users to enter text and conduct a job search on a website. While most of the functionality is in place, including the reset button, I encounter an error when attempting this.getComponent('keywords') in the submitButtonHandler function, resulting in an Uncaught TypeError.

The issue seems to stem from the lack of proper resolution for 'this' within the code block below:

var submitButtonHandler = function(btn, evt) {
    var temp = this.getComponent('keywords').getValue();
    alert(temp);
}

Here is the snippet of problematic code. Unlike the resetButtonHandler above, this code fails to resolve 'this' correctly.

var resetButtonHandler = function (btn, evt) {
this.getComponent('keywords').reset();
this.getComponent('dposted').reset();
this.getComponent('jtitle').reset();
this.getComponent('jcategory').reset();
this.getComponent('salaryf').reset();
this.getComponent('salaryt').reset();
this.getComponent('jscategory').reset();
this.getComponent('ptype').reset();
}

The following section simply defines the structure of the form:

SearchJobsForm.form = Ext.extend(Ext.Panel,{

    initComponent: function(){

        Ext.apply(this, {
            floating: true,
            width: 250,
            height: 370,
            scroll: 'vertical',
            centered: true,
            modal: true,
            hideOnMaskTap: false,
            items: [{  
            // Form fields go here
            },{
                // Additional form fields
            }],
            dockedItems: [{
                xtype: 'toolbar',
                itemId: 'toolbar',
                dock: 'bottom',
                height: '36',
                items: [
                    { 
                        xtype: 'button', text: 'Reset',itemId: 'reset',scope: this,
                        handler:  resetButtonHandler  
                    },
                    { 
                        xtype: 'spacer'
                    },
                    { 
                        xtype: 'button', text: 'Submit',
                        handler: submitButtonHandler 
                    }
                ]
            }]
        });

        SearchJobsForm.form.superclass.initComponent.call(this);

        this.items.get(2).on({
            change: this.onChange,
            scope: this
        });
    },

    onChange: function(selectField, value){
        this.items.get(1).disable();
    }

});

Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'icon.png',
    glossOnIcon: false,
    onReady: function(){
        var form = new SearchJobsForm.form();
        form.show();                
    }
});

Answer №1

When using this.getComponent(childId), you are browsing through the children components of the current object. In the context of resetButtonHandler(), it is important to note that this refers to the reset button.

The declaration of SearchJobsForm represents an object, not its instance. If it were an instance, the "new" command would have been used. Furthermore, SearchJobsForm.fieldName does not refer to a field.

Therefore, within the submitButtonHandler() method's scope, this can be interpreted as: Ext.getCmp('reset');

You can utilize the id field (not itemId) of the component for quick access:

For instance, in SearchJobsForm.form = Ext.extend(Ext.Panel,{...

initComponent: function(){
  Ext.apply(this, {
    id: 'form', // adding an id for easy retrieval with Ext.getCmp()            
    floating: true,
    ...
  }
});

// Later on, you may use 
Ext.getCmp('keywords') 

Alternatively, navigate from the resetButton context up to the form panel and then down to the input keywords without needing to add an extra attribute:

var form = this.el.up('.x-panel');
var input_keyword = form.down('.x-input-text[name=Keywords]').getValue();

Best regards

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating variables programmatically from an array within the Ajax success callback

Hi there, I am currently working with an AJAX call and have the following code: $.each(data.my_json, function(i, item) { const op_id = item.op_id; const op_name = item.op_name; let nv_tmp_totals = item.nv_tmp_totals; let nvc_tmp_totals = ...

What causes tests to fail with an error message SyntaxError: Unexpected token {?

Hey there! I'm encountering an issue with webpack 5 and babel while trying to run tests that were previously written. Despite following the jest documentation for configuration, I haven't been able to find a solution after exploring various forum ...

Controller unable to properly increment values

$scope.isChecked = function(id){ var i=0,j=0,k=0; //$scope.abc[i].usertype[j].keywords[0].key_bool=true; if($scope.abc[i].type_selected == true){ while($scope.abc[i].usertype.length){ while($scope.abc[i].userty ...

JavaScript slowness

Currently, I am developing a test page that consists of buttons triggering various scripts. One of the functionalities I am trying to implement is changing the background color every second for 5 seconds, cycling through a total of 5 different colors. Desp ...

Showing the URL beyond the search bar: A Guide using PHP, JavaScript, and HTML

How can I display the URL link outside the search box instead of opening a new page with the search result? I want to show the full URL (https://www.php.net.) below the search box, not within the search results. I only want to see the URL, not the contents ...

Issue with JQuery's click and submit events not triggering, but change event is working fine

My webpage has a dynamic DOM that includes add and save buttons to interact with elements. Each row also has a remove option for deletion. When a user logs in, the controller redirects them to their homepage in the codeigniter PHP framework. This controlle ...

Unable to delete React element by ID as it is undefined

Having some trouble deleting an item by ID with React. Despite the backend routes functioning properly (node and postgresql), every attempt to delete an item results in it reappearing upon page refresh. The command line indicates that the item being delete ...

The image fails to load smoothly in the midst of an ajax operation

Here is my code snippet... div used for displaying content along with a loading image: <div id="adS" style="float: left; width: 70%"> <div id="loading-image" style="background-image: url('images/processing.gif'); display: none;"> ...

What is the best way to attach a Label to a THREE.Mesh object?

I'm looking to show the name of a Three.js Three.Mesh as a label when hovering over the mesh. Does anyone know how to achieve this in Three.js? Could someone provide an example code snippet for this? ...

What is the best way to move an element to a different position?

I'm currently practicing JavaScript and Dynamic HTML through a course I'm taking. While checking my code in Firefox and Chrome, I haven't come across any errors or warnings so far. However, there might be something I'm missing or doing ...

The Vue.js application is experiencing issues with displaying Google Maps functionalities

I have developed an application using Vue.js in Monaca and Cordova with onsenUI. My goal is to display my location on a Google map within the page. I attempted to achieve this by utilizing the npm package named vue2-google-maps, but unfortunately, it' ...

`Interact with images to display toggled information using a combination of CSS and JavaScript

Is there a way to make the images on my portfolio clickable so that more information about the project appears on the first click, and then hides when clicked again? Preferably using simple JavaScript without jQuery. I found a website where this feature i ...

How can the title of a link be hidden in a popup panel?

Seeking assistance for a popup panel issue. When you click on the black ffff button, a popup panel appears with the text ffff having a blue shadow in the top left corner that disappears when clicked. How can I remove this? Here's the link: <a class ...

How do I adjust brightness and contrast filters on a base64 URL?

When presented with an image in base64 format like this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDAAwfqNRk/rjcYX+PxrV/wtWqwJSTlboMgAAAABJRU5ErkJggg== What is the most efficient method to programmatically alter a filter (such as brightness or cont ...

Can you explain the concept of a framework operating "on top of" node.js in a way that would be easy for a beginner to understand?

If someone is new to JavaScript, how would you explain the concept of "on top of node.js" in simple programming language? I am looking for a general explanation as well as specific reference to Express on top of node.js in the MEAN stack. Appreciate your ...

What is the best way to display data retrieved from a GET request in Angular?

Spending too much time on a development issue is causing me frustration. After extensive research, I find myself stuck at this point. The problem lies in making a GET request from a service which is called by a controller. Below is the code for the servi ...

Retrieve and process information retrieved from an Ajax call in ASP.NET using AJAX

When I receive a list of data from an Ajax call, it looks like this. $(document).ready(function () { var hashtag = 'dilwale' var accessToken = '16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82' $.ajax({ url: ' ...

Exploring the boundaries of React's useContext functionality

In my applications, I specialize in creating custom hooks for accessing state stores. For example, I typically define the hook like this: const store = new Store(); const StoreContext = createContext(store); StoreContext.displayName = "StoreContext"; fun ...

Is it possible to use setInterval in Node.js only while a specific variable remains true?

I am working with a setInterval function and an isRunning variable. Can someone please advise me on how to appropriately stop the setInterval when the isRunning variable is set to false? Thank you in advance for your help! ...

Creating dynamic class fields when ngOnInit() is called in Angular

I am trying to dynamically create variables in a class to store values and use them in ngModel and other places. I understand that I can assign values to variables in the ngOnInit() function like this: export class Component implements OnInit{ name: st ...