jquery插件开发:原型回调参数
jquery plugin development : prototype callback arguments
我从来没有真正开发过jquery插件,我在这个概念上有所欠缺。。。我正在尝试修改这个图像裁剪插件,以便有一个新的回调来动态设置纵横比。
因此,在cropper.js中,我添加了一个回调:
Cropper.prototype = {
construstor: Cropper,
//init function with init code
//My new function
setAspectRatio: function(ratio) {
console.log(ratio);
this.disable();
this.defaults.aspectRatio = ratio;
this.enable();
},
//more functions here ...
}
在我看来:
var $cropper = $(".cropper");
//call the cropper
$cropper.cropper({ aspectRatio: 580 / 280 });
//change the ratio
$cropper.cropper("setAspectRatio",1600/585);
但该比率不会传递给回调。console.log()输出未定义的结果。
我在这里错过了什么?
为了能够从jQuery集合方法向实例方法传递参数,您需要更改行
…
if (typeof options === "string" && $.isFunction(data[options])) {
data[options]();
}
…
在$.fn.cropper
中(在文件的最后)到
// Register as jQuery plugin
$.fn.cropper = function(options) {
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function() {
var $this = $(this),
data = $this.data("cropper");
if (!data) {
data = new Cropper(this, options);
$this.data("cropper", data);
}
if (typeof options === "string" && $.isFunction(data[options])) {
data[options].apply(data, args);
}
});
};
相关文章:
- 要求未定义JS回调参数
- Axios spread()具有未知数量的回调参数
- 节点回调参数无法断言instanceof Error
- each()-将额外的参数传递给回调参数
- AngularJS tokenWrapper-错误的回调参数
- 拉斐尔未运行回调参数
- 使用 jQuery 回调参数通过 AJAX 调用 PHP 函数
- 将函数的执行同步为 node.js 中的回调参数
- 当第一个函数完成没有回调参数时执行第二个函数
- 使用回调参数从函数中获取值
- 如何使用可选的回调参数制作 javascript 函数
- 为什么设置超时's回调参数可以接受参数
- 对 Array.prototype.some 使用额外的回调参数
- 使用Google闭包编译器记录回调参数
- JavaScript Object Literal“;这个“;关键字jQuery回调参数传递
- 在Meteor中运行的回调中未定义回调参数
- 回调参数名称
- 将jQueryajax回调参数定义为现有对象类型
- Reactjs路由器匹配回调参数总是未定义的
- Javascript自定义事件更改回调参数