开发者

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.Plugin开发者_运维知识库Manager.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().


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

})();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜