2

I have common code that I want in various events for our standard jqGrid configuration.

I use $.extend($.jgrid.defaults, { }); to set some code for the following:

  • loadError()
  • beforeRequest()
  • loadComplete()
  • gridComplete()

Now, when I define an instance of my grid, there is a potential that I would like to execute more code in any of these events.

My initial solution was to have global variables, e.g.: loadCompleteEx() and if they are defined, call them at the end of the default method calls. This was great if I only had one grid on the page, but I am trying to implement a solution that would work regardless of how many grids are on the page.

Is there some way to hook in and add a method to execute when any of these events are fired? I'm using jqGrid 4.1.2 and jQuery 1.6.

Thanks in advance for your help!

IronicMuffin
  • 4,182
  • 12
  • 47
  • 90

1 Answers1

1

You can .bind() your own functions to events, so that your function executes with the event. You could use this to bind different events to each grid, or bind one function to every grid at once.

.bind() also allows binding to standard javascript events, custom events, or even undefined strings which you later call with .trigger().

It needs to be attached after a selector, which can select a specific grid or multiple grids in your case.

$('#Your-Grid-Selector').bind('eventToBind', functiontoBind() {
            alert('Triggered eventToBind');
        });

jQuery Documentation

Edit: This question may be a duplicate of this SO Question, which has a good solution you can use for this very issue.

Community
  • 1
  • 1
Kyeotic
  • 19,697
  • 10
  • 71
  • 128
  • I don't know if they are actual jQuery events. The functions that I am setting are executed in the jqGrid code using `loadComplete.call()` syntax. Can I bind to fire when a particular function executes, e.g.: `$.bind('loadComplete',myFuncToRun);`? – IronicMuffin Aug 31 '11 at 15:25
  • [jqGrid Source](https://github.com/tonytomov/jqGrid/blob/master/js/grid.base.js) for reference. – IronicMuffin Aug 31 '11 at 15:26
  • I have not tried that specific plugin, but bind works with the custom events of other plugins, like jsTree. if jqGrid creates them as events it will work. You could give it a try and report back. – Kyeotic Aug 31 '11 at 15:43
  • Wow I am an idiot. I even looked through my previous questions to see if I asked this before... Thanks. – IronicMuffin Aug 31 '11 at 16:04
  • Lol, I didn't even realize that other one was from you. – Kyeotic Aug 31 '11 at 16:17