What is the best way for my controller to access the appropriate view component in extjs?

In my project, I have defined a Ext.grid.Panel named JobList, which includes an Ext button with the unique identifier myButton. The JobList also has its own controller. Within the controller, I have incorporated the following code:

Ext.define('App.controller.JobList', {
  extend: 'Ext.app.Controller',
  refs: [
    {ref: 'jobList', selector: '#jobList'},
    {ref: 'myButton', selector: '#myButton'}
  ],
  init: function(){
    this.control({
      'jobList': {
        select: this.selectJob
      }
    });
  },
  selectJob: function(){
    this.getMyButton().enable();
  }
});

Now, when I instantiate two instances of JobList using Ext.create and assign them the IDs jobList1 and jobList2, a problem arises. When I select a job on jobList2, it ends up enabling the myButton on jobList1 instead of on jobList2. How can I ensure that each instance of JobList correctly enables its own myButton?

Answer №1

It is recommended to avoid referencing by itemId and instead use aliases for better organization:

// Ensure that in your App.view.JobList.js file, the following code is included
Ext.define('App.view.JobList', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.job-list',
    // ...
    dockedItems: [{
        xtype: 'button',
        name: 'myButton',
        text: 'My button',
    }]
});

// Then in the App.controller.JobList.js:
    // ...
    'job-list': {
        selectionchange: function(model, selected) {
            var button = model.view.up('job-list').down('button[name=myButton]');
            button.setDisabled(Ext.isEmpty(selected));
        }
     }

To see a working example, visit:

Answer №2

Utilizing a global controller allows you to capture events from all views that meet the specified criteria. To avoid issues like this, consider implementing the MVVM pattern in extjs5. Sencha has done an excellent job with MVVM, as each instance of a view has its own ViewController, preventing conflicts like the one described. If you prefer sticking to the MVC pattern, you will need to handle these situations manually. Refrain from using refs if your view class has multiple instances. Instead, retrieve other components by querying within your current component. For example:

Ext.define('App.controller.JobList', {
    extend: 'Ext.app.Controller',

    init: function() {
        this.control({
           'jobList': {
               select: this.selectJob
           }
        });
    },

    selectJob: function(selectionModel){
        //To access the grid, use the selectionModel linked to our grid in some way
        var grid = selectionModel.view.ownerCt; //or find a more elegant way to get the grid from selectionModel
        var button = grid.down('#myButton');
        button.enable();
    }
});

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

combine multiple select options values in a single function using jQuery

My HTML code includes two select options for users to choose the origin and destination cities. I need to calculate the cost of travel between these cities. How can I compare the selected options using jQuery? </head> <body> <div> ...

Trouble with mouseout listener in Wordpress causing Google Map to malfunction

Having trouble integrating a map into my WordPress site using the Google Maps API v3. The issue I am facing is that the listener assigned to the mouseout event is not functioning properly. I have copied and pasted the code from another website where it was ...

error": "message": "Property 'name' cannot be read because it is undefined

I've encountered an issue while creating a route to handle POST data. Despite testing it on postman, I have not been able to find a solution for the problem that many others seem to be facing as well. It seems like the 'name' field is not be ...

Using a loop in a Vue.js slick slider: easy step-by-step guide

While utilizing vue-slick link https://www.npmjs.com/package/vue-slick within a bootstrap modal, I encountered an issue when using a v-for loop to iterate through the items. The problem can be seen in this example: https://i.sstatic.net/PhJCE.jpg. Below i ...

JavaScript is failing to add items to an array

Attempting to add coordinates and user-specified data from a form to an array named "seatsArray". Here's the code snippet: <div> <img onLoad="shiftzoom.add(this,{showcoords:true,relativecoords:true,zoom:100});" id="image" src="plan1.bmp" wid ...

What is the best way to display SVGs from an API response using next/image or the <img> tag in Next.js?

I have a collection of SVGs being returned from an API response, which I am fetching inside the useEffect. The response structure is as follows: {svg1: 'https://remoteserver.com/assests/svg1.svg', ...and so on} (These SVGs are not static assets ...

Combine Angular variable in JavaScript function

I want to combine "proposal.id" in the initial parameter of the window.open function. Here is my current code snippet: <button ion-button color="light" onclick="window.open('https://mywebsite.com/offer?id={{ proposal.id }}', '_self' ...

What is the best way to enhance a state's capabilities in Machina.js?

When using Machina.js (version 0.3.6), how can I instantiate a modified FSM constructor where both the child and parent FSMs define behaviors in the same states? Here is the code snippet: var _ = require('lodash'); var machina = require('m ...

Looking to reload a div with a captcha in a contact form without the need to refresh the entire page

I have integrated a div tag into my contact form to contain the captcha elements. If the user is unable to read the captcha image, I want to provide them with an option to refresh that specific section without reloading the entire page. I have tried variou ...

The significance of my values fluctuates within the datepicker, at times acknowledged and other times

Here's the situation in a nutshell: <script> //I decided to make things easier for myself by hard coding these values var startDate = '01-01-2013'; var endDate = '01-31-2013'; $(document).ready(function() { $('#R ...

Executing a pair of queries within the same table and integrating them within a unified route

How can I efficiently execute two queries on the same table in my project? I have been considering using promises, but my knowledge on them is limited. Even though I've researched about it, I am struggling to implement the correct structure. My main ...

Run the GetServerSideProps function every time the button is clicked

Struggling with my NextJS app development, I encountered an issue while trying to fetch new content from an API upon clicking a button. The server call is successful, but the update only happens when I refresh the page rather than triggering it with the bu ...

The dynamic duo of web development: React and Bootstrap JavaScript

As I work with ReactJS, I have come to understand that using JQuery or vanilla JS to directly manipulate the DOM is not recommended. This is because ReactJS operates using a virtual DOM, which can lead to unpredictable outcomes. My question now is: if I w ...

Directly insert an image into your webpage by uploading it from the input field without the need to go through the server

Can an image be uploaded directly from <input type="file" /> into an HTML page, for example through the use of Javascript, without needing to first save the image to the server? I am aware that AJAX can accomplish this, but if there is a way to ...

Having trouble getting Bootstrap's Javascript to function properly in a Rails 6 webpacker configuration?

Recently initiated a brand new Rails 6.0.3 project and integrated jQuery and Bootstrap into it. Surprisingly, jQuery is functioning smoothly, and the Bootstrap CSS appears to be rendering correctly. However, every attempt to execute $.fn.tooltip.Construc ...

Viewify - displaying images in v-data-table cells reveals only a portion of the image

I have a challenge with displaying avatars for users in a table. The rows are too small and the images are getting cut off. https://i.sstatic.net/QjkHj.png Table: <v-data-table :headers="tableHeaders" :items="streamers"> <t ...

Despite being listed in the entry components, HelloComponent is not actually included in the NgModule

Check out my StackBlitz demo where I am experimenting with dynamically instantiating the HelloComponent using the ReflexiveInjector. The HelloComponent is added to the app modules entryComponents array. Despite this setup, I am still encountering the foll ...

submit multiple images simultaneously

I'm attempting to upload multiple photos simultaneously using XMLHttpRequest. if(files.length <= 6) { for(var i = 0; i < files.length; i++) { var formData = new FormData(); formData.append('action', 'upload ...

The best method for quickly loading a webpage that is over 20MB in size

My website is a single-page calendar featuring a full year's worth of images. With 344 requests and a total load size of 20MB, the page consists of a simple PHP script without a database. The requests include a CSS file (15KB), 4 JS files (20KB), two ...

Tips for avoiding the persistence of an old array on the screen after refreshing and showing the new, updated array

Currently, my task involves displaying array values on a webpage. The array data is sourced from a real-time database in Firebase. After adding new values to the array or inputting another value into the database on the previous page, we are redirected to ...