调用自定义 jquery 插件中的方法
我尝试构建我的第一个插件。我已经走到这一步了:
(function( $ ){
var settings = {
key1: 't1',
key2: 't2'
};
var methods = {
init : function( options ) {
return this.each(function() {
var $this = $(this);
console.log('init called');
data = $this.data('snake');
// If the plugin hasn't been initialized yet
if (!data) {
//Do setup stuff here
$this.data('snake', {
map: $this.find(".map"),
stats: $this.find(".stats")
});
data = $this.data('snake');
}
if ( options ) {
$.extend( settings, options );
}
// HERE I WOULD LIKE TO CALL THE RUN METHOD AND BE ABLE TO USE settings AND data VARIABLES
});
},
run : function( ) {
var $this = $(this), data = $this.data('snake');
console.log('run called');
//test for settings and data
console.log(settings);
console.log(data);
},
test : function( ) {
return this.each(function() {
var $this = $(this), data = $this.data('snake');
console.log('test called');
//test for settings and data
console.log(settings);
console.log(data);
});
},
setup : function ( ) {
console.log('setup called');
},
hide : function( ) {}
};
$.fn.snake = function( method ) {
// console.log('call: ' + method);
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
})( jQuery );
现在我需要从 init
方法中调用 run
方法。我怎样才能做到这一点?
I try to build my first plugin. That far I came yet:
(function( $ ){
var settings = {
key1: 't1',
key2: 't2'
};
var methods = {
init : function( options ) {
return this.each(function() {
var $this = $(this);
console.log('init called');
data = $this.data('snake');
// If the plugin hasn't been initialized yet
if (!data) {
//Do setup stuff here
$this.data('snake', {
map: $this.find(".map"),
stats: $this.find(".stats")
});
data = $this.data('snake');
}
if ( options ) {
$.extend( settings, options );
}
// HERE I WOULD LIKE TO CALL THE RUN METHOD AND BE ABLE TO USE settings AND data VARIABLES
});
},
run : function( ) {
var $this = $(this), data = $this.data('snake');
console.log('run called');
//test for settings and data
console.log(settings);
console.log(data);
},
test : function( ) {
return this.each(function() {
var $this = $(this), data = $this.data('snake');
console.log('test called');
//test for settings and data
console.log(settings);
console.log(data);
});
},
setup : function ( ) {
console.log('setup called');
},
hide : function( ) {}
};
$.fn.snake = function( method ) {
// console.log('call: ' + method);
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
})( jQuery );
Now I need to call the run
method from within the init
method. How do i achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊抱歉伙计们 - 我现在找到了答案。看:
Ah sorry dudes - i found the answer now. Look: