views:

120

answers:

2

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);
})();
+5  A: 

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().

SLaks
+1 i like your answer, very good alternatives! Alsciende's solution is a little closer to my needs, so get the accepted mark (for me)
avastreg
+1  A: 

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);

})();
Alsciende