How can i call that function inside that anonymous javascript? (TinyMce example)

How can i call test() inside that method? It's possible?

(function() {

    tinymce.create('tinymce.plugins.WrImagerPlugin', {

        init : function(editor, url) { 

            editor.addCommand('mceWrImagerLink', function() {
                //--> how can i refer to test() here?
            });
        },
        test: function () {alert('test');}
        }
    });

    tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();

Answers


You can also keep a reference to this in the init method that will be available in the addCommand closure:

(function() {

tinymce.create('tinymce.plugins.WrImagerPlugin', {

    init : function(editor, url) { 
        var me = this;
        editor.addCommand('mceWrImagerLink', function() {
            //--> how can i refer to test() here?
            me.test();
        });
    },
    test: function () {alert('test');}
    }
});

tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);

})();

You can make test a regular function and assign it to the object, like this:

(function() {
    function test() { alert('test'); }

    tinymce.create('tinymce.plugins.WrImagerPlugin', {
        init : function(editor, url) { 
            editor.addCommand('mceWrImagerLink', function() {
                test();
            });
        },
        test: test
    });

    tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();

Alternatively, you can keep a reference to the object:

(function() {
    var wrImaergPlugin = {    
        init : function(editor, url) { 
            editor.addCommand('mceWrImagerLink', function() {
                wrImagerPlugin.test();
            });
        },
        test: function() { alert('test'); }
    }

    tinymce.create('tinymce.plugins.WrImagerPlugin', wrImagerPlugin);
    tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();

Finally, in this specific case, you should be able to simply call tinymce.plugins.WrImagerPlugin.test().


Need Your Help

Optimize mysql query Suppliers <-> Products <-> Categories with DISTINCT

mysql sql select join query-optimization

My DB is like this: Products are N:N to Categories so there is a join table (products_categories). Products have a Supplier (1 and only 1), so Products table have a supplier_id.

Pandas Efficient Way to Calculate Annual Inventory Based on Equipment Date Ranges

python pandas

I'm looking to translate a dataframe of equipment date ranges and characteristics into their annual total install time by characteristic groupings. I'm looking to translate a dataframe like this: