How to grab reference to Editor instance
How to grab reference to Editor instance
How do I get a reference to an Editor instance wherever I'm at in my application? Like, how do I grab the reference from the DOM
, window
, or document
?
I'm experimenting with modules. I have a table/editor instance in its own "module" which I got from the Download Builder.
I can successfully pass the editor
reference/instance around through functions like a hot potato. So, that's cool, and it works!
Example:
//this is a module with Editor and Table
//add button calls a function in another module
buttons: [
{
extend: "create",
text: "Add",
editor: editor,
action: function (e, dt, node, config) {
renderRetireeCreateModal(editor); //passing in a reference to the editor instance
}
},
//this module accepts the instance, and I can call .create()
function addRetireeListener(modalActual, editor) {
...
editor
.create(false)
.set('EmployeeID', jsonRequest.EmployeeID)
.submit();
}
I guess I could also assign a global variable, like var window.editor = new $.fn.dataTable.Editor
, but I heard somewhere that it is considered uncouth to add a bunch of stuff to the global namespace.
Sooooo..... How do I grab the Editor instance from wherever I'm at?
This question has accepted answers - jump to:
Answers
You basically have two options:
I don't know the structure of your code without being able to see it all, but you mention modules - I'm guessing those are ES6 modules, so you might
export
youraddRetireeListener
function and thenimport
it wherever it is you need to call it and have the Editor licenser. That I think would be the best option, but it really depends upon the stack you are using?Allan
@allan
I'm using vanilla JavaScript.. You got me thinking...
I often use the Download Builder, but I modify the
table.Tablename.js
to fit my needs. (example below).How would you recommend to properly export Editor and Table so that they could be used in other modules, but also take care of initializing the tables at startup?
If I could reference
editor
ortable
via the export functionality, that would be pretty darn cool!!This is an example of what I do currently with the file provided by Download Builder.
initRetireeDashboard()
is called in my main module, which initializes the tables when the page loads...You could return it from your
initRetireeDashboard
function - e.g:then:
Allan