Get editor reference within editor ajax function?

Get editor reference within editor ajax function?

aaron.dunigan.atleeaaron.dunigan.atlee Posts: 20Questions: 7Answers: 0

I'm using Editor with the ajax property set as a function. Within this function, is there any way to get a reference to the editor instance that called it? I don't think so based on the parameters passed, but maybe someone else has some insight?

(I realize I can declare the editor instance as a global variable, which is what most of the examples do, and what I'm doing now, but there are reasons for wanting to avoid doing this if not necessary.)

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    Answer ✓

    You could do:

    var editor = new DataTable.Editor({
      ajax: function ( ... ) {
        editor.field( ... ) // etc
      }
    });
    

    That would work because the ajax function will not be executed during Editor's start up. Thus the editor variable will be assigned by the time the ajax function executes, and can be accessed correctly. No need for a global - this works with just the scope of whatever you are building the Editor instance in.

    Allan

  • aaron.dunigan.atleeaaron.dunigan.atlee Posts: 20Questions: 7Answers: 0

    @allan Thanks for the response, that's a concise way to handle the issue and works for me for now; however, I'm working on a quirky platform that doesn't allow for ajax URL's, so I'm envisioning creating a single general ajax function that all of my editor instances call, which then handles the back-end mechanics. It's in this case where I may find it useful to know which instance called the function. Please consider this a feature request for some future version.

    (I do realize that even in that case I can do something like the following, so asking the API to pass the editor instance is really just a convenience that makes the code a little cleaner.)

    var editor = new DataTable.Editor({
      ajax: function ( method, url, data, success, error ) {
        generalAjaxHandler( method, url, data, success, error, editor ) 
      }
    });
    
  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    edited September 2022 Answer ✓

    Hi,

    I've been mulling this over a bit since your initial question and I think it would be entirely reasonable to expect this inside the ajax function to have the scope of the Editor instance - so:

    var editor = new DataTable.Editor({
      ajax: function ( method, url, data, success, error ) {
        generalAjaxHandler( method, url, data, success, error, this ) 
      }
    });
    

    would be perfectly valid.

    It doesn't do that at the moment, but I will change that for the next release. In the meantime, if you wanted to tweak your local install of Editor for this, find:

    ajaxSrc(null, null, data, success, error);
    

    and replace with:

    ajaxSrc.call(this, null, null, data, success, error);
    

    which will do the job nicely.

    Regards,
    Allan

  • aaron.dunigan.atleeaaron.dunigan.atlee Posts: 20Questions: 7Answers: 0

    Thanks @allan , you're a rock star! I appreciate how much time you must put into this tool, and its capabilities continue to amaze me.

Sign In or Register to comment.