如果我遵循plugin authoring guide,如何从公共方法调用私有方法,反之亦然?
我通常在init
方法中声明私有方法,如:
var methods = {
init: function(options) {
var settings = $.extend({
}, options);
return this.each(function() {
var $this = $(this);
var data = $this.data('griffin-editor');
this.trimSpaceInSelection = function () {
//how do I call a public method here?
//to get the this context correct.
}
if (typeof data !== 'undefined') {
return this;
}
//the rest of the code.
这可能是不正确的事情吗?
答案 0 :(得分:1)
如果通过'this context correct',你的意思是你想调用一些公共方法,将这个set设置为值,这里面有trimSpaceInSelection,那么你就可以这样做:
....
this.trimSpaceInSelection = function () {
methods.somePublicMethod.apply(this, arguments); // this will pass all arguments passed to trimSpaceInSelection to somePublicMethod
}
....
如果你想在公共方法中设置这个当前的jQuery集合,那么:
....
this.trimSpaceInSelection = function () {
methods.somePublicMethod.apply($this, arguments); // this will pass all arguments passed to trimSpaceInSelection to somePublicMethod
}
....