Extending events in a view

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BattleMetalChris
    Member
    • Mar 2021
    • 51

    Extending events in a view

    I'm extending a view, the original of which has a list of click events:


    Code:
    events: {
       'click [data-action="run"]': function () {
            this.run();
            this.afterRun();
       },
       'click [data-action="refresh"]': function () {
          this.run();
       },
       'click [data-action="printPdf"]': function () {
          this.actionPrintPdf();
       },
    
    ..... plus a bunch more

    I want to add the following to this list in my extended view

    Code:
    'click [data-action="exportSpecialReport"]': function () {
        this.actionExportSpecial();
    }
    ​
    While also preserving the original list from the parent.

    If I add just my new click event in the new view then it overwrites the parent's events object, so none of the original click events work.

    I can make it work by copying the code for the whole event object from the parent .js file and adding my new entry on the end, but this isn't great. Is there any way to add an item to the object without having to redefine everything?
  • Kharg
    Senior Member
    • Jun 2021
    • 439

    #2
    When you extend a view that already has its events and you want to add more events, do it the following way:
    PHP Code:
    setup() {
    super.setup();
    
    this.events['click a[data-action="test"]'] = (e) => {
    
    };
    }


    Comment

    • yuri
      Member
      • Mar 2014
      • 8627

      #3
      Use addHandler https://docs.espocrm.com/development/view/#addhandler. The events property is not recommended.
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      • BattleMetalChris
        Member
        • Mar 2021
        • 51

        #4
        yuri that's much tidier, thanks.

        However, if I put it in the setup property, it completely overrides that property in the parent view - how do I call the parent view's setup method from my extended one?

        Comment

        • yuri
          Member
          • Mar 2014
          • 8627

          #5
          It should not override. How exactly you define the event and setup method.
          If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

          Comment

          • BattleMetalChris
            Member
            • Mar 2021
            • 51

            #6
            I'm extending the list report in the Advanced Pack to add a new item in the dropdown (which is in the 'specialreport' template)

            Code:
            define('advanced-patch:views/report/reports/list', ['advanced:views/report/reports/list'], function (Dep) {
            
                return Dep.extend({
            
                  template: 'advanced-patch:report/reports/specialreport',
            
                  setup: function(){
            
                     this.addHandler('click', '[data-action="exportSpecialReport"]', (event, target) => {
                        this.actionExportPFC();
                     });
                  },
            
               });
            });
            With the setup property in there, the setup function in advanced:views/report/reports/list never gets called. If I remove the property from my view, it gets called in the parent as normal.

            Comment

            • yuri
              Member
              • Mar 2014
              • 8627

              #7
              Code:
              setup: function () {
                  Dep.prototype.setup.call(this);
              },
              If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

              Comment

              • BattleMetalChris
                Member
                • Mar 2021
                • 51

                #8
                Awesome thanks! That works perfectly now.

                Comment

                Working...