1. #1
    Sencha User env0ke's Avatar
    Join Date
    Apr 2011
    Posts
    57
    Answers
    2
    Vote Rating
    1
    env0ke is on a distinguished road

      0  

    Question Answered: Controller function with params

    Answered: Controller function with params


    Hi all,

    I try create function in controller of my app:

    Code:
    init:function () {        this.control({
                '#all-incidents-grid button[action=edit]':{
                    click:this.editIncident()
                }
            });
        },
    ...
    editIncident:function () {
            Ext.widget('EditWindow');
            this.getEditForm().loadRecord(this.getAllGrid().getSelectionModel().getSelection()[0]);
            this.getEditWindow().show();
        }
    It working, but i need a function with param for reusable:

    Code:
    init:function () {        this.control({
                '#all-incidents-grid button[action=edit]':{
                    click:this.editIncident(this.getAllGrid())
                }
            });
        },
    ...
    editIncident:function (grid) {
            Ext.widget('EditWindow');
            this.getEditForm().loadRecord(grid.getSelectionModel().getSelection()[0]);
            this.getEditWindow().show();
        }
    It not working .

  2. you can put a "forwarding" method between them.
    like:

    Code:
    click: function(default params here) {
        this.myFunction(myCustomParam,...);
    },
    scope: this

  3. #2
    Sencha - Services Team arthurakay's Avatar
    Join Date
    Sep 2008
    Location
    Antioch, IL
    Posts
    1,352
    Answers
    60
    Vote Rating
    32
    arthurakay is a jewel in the rough arthurakay is a jewel in the rough arthurakay is a jewel in the rough

      0  

    Default


    Quote Originally Posted by env0ke View Post
    Code:
    init:function () {
            this.control({
                '#all-incidents-grid button[action=edit]':{
                    click:this.editIncident(this.getAllGrid())
                }
            });
        },
    ...
    editIncident:function (grid) {
            Ext.widget('EditWindow');
            this.getEditForm().loadRecord(grid.getSelectionModel().getSelection()[0]);
            this.getEditWindow().show();
        }
    First, you don't want to do this:
    Code:
    click:this.editIncident(this.getAllGrid())
    That sets the "click" event to the result of your function. You want to set the click event to the function itself:

    Code:
    click:this.editIncident //NO PARENTHESES!
    Second, why do you need to pass the argument? Just access that variable inside the handler:

    Code:
    editIncident:function () {
            var grid = this.getAllGrid();
            Ext.widget('EditWindow');
            this.getEditForm().loadRecord(grid.getSelectionModel().getSelection()[0]);
            this.getEditWindow().show();
        }
    The only other thing you might have to do is change the "scope" of the event handler so that "this" is the correct object.
    Arthur Kay
    Developer Relations Manager, Sencha Inc.

    Twitter | Sencha Chicago User Group

  4. #3
    Sencha User env0ke's Avatar
    Join Date
    Apr 2011
    Posts
    57
    Answers
    2
    Vote Rating
    1
    env0ke is on a distinguished road

      0  

    Question


    arthurakay thx for your answer.

    If I have a general class of the grid and I want to create 5 objects of this class and ask each of them similar function handler clicks. I have to write 5 similar functions, which will differ only in reference to the grid i want?

    Sorry of my bad english, google translate.

  5. #4
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,669
    Answers
    130
    Vote Rating
    110
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    you can put a "forwarding" method between them.
    like:

    Code:
    click: function(default params here) {
        this.myFunction(myCustomParam,...);
    },
    scope: this
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

Thread Participants: 2

Tags for this Thread