我想创建一个包含方法和回调的jQuery插件,方法有效,但我无法使回调工作,回调的范围让我困惑,
(function($)
{
var methods = {
create: function(options) {
var defaults = {
width: 320,
height: 240,
background: '#000',
onstop: function(){}
};
var options = $.extend(defaults, options);
return $(this).each(function(i) {
console.log('create');
});
},
stop: function(options) {
var defaults = {
onstop: function(){}
};
var options = $.extend(defaults, options);
return $(this).each(function(i) {
console.log('stop');
options.onstop.call();
});
}
};
$.fn.myplugin = function(option) {
if (methods[option]) {
return methods[option].apply( this, Array.prototype.slice.call(arguments, 1));
} else if (typeof option === 'object' || ! option) {
return methods.create.apply(this, arguments);
} else {
$.error('Method ' + option + ' does not exist in jQuery.plugin');
}
};
})(jQuery);
所以在<script>
:
$(document).ready(function(){
$('#start').click( function(){
$('#myvideo').myplugin('create', { onstop: function(){ console.log('on stop'); } });
});
$('#stop').click( function(){
$('#myvideo').myplugin('stop');
});
});
基本上我似乎想让onstop:function(){}全局到插件
/ * ========更新代码(见评论)======== * /
(function($)
{
var callbacks = {
onready: $.Callbacks("once memory unique").add(function() {
//console.log('onready');
}),
ondeny: $.Callbacks("unique").add(function() {
//console.log('ondeny');
}),
onerror: $.Callbacks("unique").add(function() {
//console.log('onerror');
}),
onstop: $.Callbacks("unique").add(function() {
//console.log('onstop');
})
};
var methods = {
construct: function(){
},
create: function(options) {
var defaults = {
width: 320,
height: 240,
background: '#000',
onready: function(){},
onstop: function(){}
};
var options = $.extend(defaults, options);
return this.each(function(i, elements) {
for (var prop in options) {
if (prop in callbacks) {
callbacks[prop].add(options.prop);
}
}
callbacks.onready.fire();
});
},
stop: function() {
return this.each(function(i, elements) {
callbacks.onstop.fire();
});
}
};
$.fn.myplugin = function(option) {
if (methods[option]) {
return methods[option].apply( this, Array.prototype.slice.call(arguments, 1));
} else if (typeof option === 'object' || ! option) {
return methods.construct.apply(this, arguments);
} else {
$.error('Method ' + option + ' does not exist in jQuery.plugin');
}
};
})(jQuery);
答案 0 :(得分:1)
我建议使用一些完整的jQuery.Callbacks
对象:
var callbacks = {
onready: $.Callbacks("once memory unique").add(function() {
console.log('ready');
}),
ondeny: $.Callbacks("unique").add(function() {
console.log('deny');
}),
onerror: $.Callbacks("unique").add(function() {
console.log('error');
}),
onstop: $.Callbacks("unique").add(function() {
console.log('stop');
})
};
现在,在获得一些新选项时,你可以做到
for (var prop in options)
if (prop in callbacks)
callbacks[prop].add(options[prop]);
要调用它们,只需执行
callbacks.onstop.fire(/*args*/);
或者,如果您关心回调的上下文,请使用fireWith
。
答案 1 :(得分:0)
我使用了globalOptions变量将插件初始化选项存储为全局
这是两个用来测试并用作stencyl的文件。
jquery.plugin.js (函数($){
var pluginName = 'myPlugin';
var globalOptions = '';
var methods = {
create: function(options) {
var defaults = {
backgroundColor: '#000',
color: '#fff',
oncreate: function(){},
onchangecolor: function(){},
onloadpage: function(page){}
};
globalOptions = $.extend(defaults, options);
return $(this).each(function(i, elements) {
$(elements).data('globalOptions', globalOptions);
globalOptions.oncreate();
});
},
changeColor: function(){
return $(this).each(function(i, elements) {
globalOptions = $(elements).data('globalOptions');
if (!globalOptions) { $.error('Error: ' + pluginName + ' has not been initialized yet'); return false; }
$(this).css('background-color', globalOptions.backgroundColor);
$(this).css('color', globalOptions.color);
globalOptions.onchangecolor();
});
},
loadPage: function(options){
return $(this).each(function(i, elements) {
globalOptions = $(elements).data('globalOptions');
if (!globalOptions) { $.error('Error: ' + pluginName + ' has not been initialized yet'); return false; }
globalOptions.onloadpage(options.page);
location.href = options.page;
});
}
};
$.fn.myPlugin = function(option) {
if (methods[option]) {
return methods[option].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof option === 'object' || ! option) {
return methods.create.apply(this, arguments);
} else {
$.error('Error: Method "' + option + '" does not exist in ' + pluginName);
return false;
}
};
})(jQuery);
的index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Example</title>
<script src="jquery.min.js"></script>
<script src="jquery.plugin.js"></script>
<script>
$(document).ready(function() {
var myElement = $('.container').myPlugin({
color: '#555',
backgroundColor: '#ddd',
oncreate: function(){
console.log('Callback: create');
},
onchangecolor: function(){
console.log('Callback: onchangecolor');
},
onloadpage: function(page){
console.log('Callback: onloadpage = '+page);
}
});
$('.color').click(function(event){
myElement.myPlugin('changeColor');
});
$('.page').click(function(event){
myElement.myPlugin('loadPage', { page: '#test' });
});
});
</script>
</head>
<body>
<div class="container">
<button class="color">changeColor</button> <button class="page">loadPage</button>
</div>
</body>
</html>
如果有人有更好的方法来执行此操作并添加“私有”方法,请分享。 :)