Results 1 to 5 of 5

Thread: Passing listener from a view to a controller

  1. #1
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Pachuca, Hidalgo, Mexico
    Posts
    2

    Default Passing listener from a view to a controller

    Hello!
    I have the next listener in my view and it works fine, but by using the MVC how can i pass it to the controller?
    Thanks in advance

    listeners: {
    click: {
    element: 'el',
    fn: function(e, el){
    var data = Ext.get(el);
    if (data.id=='1'){
    //do something
    }
    if (data.id=='0'){
    //do something
    }
    }
    }
    }

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,448

    Default

    Since it's an element listener, you need to then fire an event on the component so it's listenable in the controller.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  3. #3
    Sencha Premium User
    Join Date
    Mar 2011
    Posts
    287

    Default

    Do you mean you want to move the logic from the view to the controller? If so, in the controller's init function, you do something like (assuming the listener example you show is on a button component):

    Code:
        init : function() {
        	this.control({
                '#buttonId': {
                	click: this.onButtonClick
                }
            });
        }
    where the onButtonClick is a function on your controller:
    Code:
        onButtonClick : function(button) {
          // do logic
        }

  4. #4
    Sencha Premium User
    Join Date
    Dec 2010
    Posts
    217

    Default Best way to find view from element?

    Hi,
    In this example the click event is fired by the DOM 'el' element. It has no ItemId so I can't handle it's events from the controller class via control(...).
    The suggestion is to bind a listener to the element 'el' and then raise a custom event.
    So, what's the best way to find the View that hosts this 'el' element?
    I want to avoid using Id's to find the view that contains the element.

    Thanks

  5. #5
    Sencha Premium User
    Join Date
    Mar 2011
    Posts
    287

    Default

    Would this work (untested by me)?

    Code:
    listeners: {
        click: {
       element: 'el', 
       fn: function(e, el) { 
           var data = Ext.get(el);
           var component = Ext.ComponentManager.get(data.id);
            var view = component.up('myView');
       }
     }
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •