传统jquery插件vs AMD jquery插件
Conventional jquery plugin vs AMD jquery plugin
我仍然无法理解这个AMD jquery插件。
// UMD dance - https://github.com/umdjs/umd
!function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
}(this, function($) {
'use strict';
// Default options
var defaults = {
};
// Constructor, initialise everything you need here
var Plugin = function(element, options) {
this.element = element;
this.options = options;
};
// Plugin methods and shared properties
Plugin.prototype = {
// Reset constructor - http://goo.gl/EcWdiy
constructor: Plugin,
someMethod: function(options) {
return options;
}
};
// Create the jQuery plugin
$.fn.plugin = function(options) {
// Do a deep copy of the options - http://goo.gl/gOSSrg
options = $.extend(true, {}, defaults, options);
return this.each(function() {
var $this = $(this);
// Create a new instance for each element in the matched jQuery set
// Also save the instance so it can be accessed later to use methods/properties etc
// e.g.
// var instance = $('.element').data('plugin');
// instance.someMethod();
$this.data('plugin', new Plugin($this, options));
});
};
// Expose defaults and Constructor (allowing overriding of prototype methods for example)
$.fn.plugin.defaults = defaults;
$.fn.plugin.Plugin = Plugin;
});
一些测试,
console.log($('.some-element').plugin({
test: 'option1',
test2: 'option2'
}));
我总是得到这个空物体,
Object[]
那么我该如何使用这个空对象呢?
我想访问插件中的方法,
var plugin = $('.element').plugin();
var instance = $('.element').data('plugin',plugin);
console.log(instance); // an empty object again!
console.log(instance.someMethod("hello world"));
TypeError:instance.someMethod不是函数
console.log(instance.someMethod("helloworld"));
那么我应该如何在插件中运行该方法呢?
它与传统的jquery插件非常不同。AMD很难理解。你知道我怎么才能让这个AMD像传统的一样工作吗!??
编辑:
最后我得到了一些东西,
var plugin = $('.element').plugin();
var instance = $('.element').data('plugin');
console.log(instance);
console.log(instance.someMethod("hello world"));
结果,
Object { element={...}, options={...}, constructor=function(), more...}
hello world
为什么那个些评论和回答的人很难指出这一点!叹气
在使用.some-element
的测试中,如果.some-element
不匹配,则会得到一个空的jQuery对象,因为$.fn.plugin
返回this.each(...)
返回的内容,而jQuery.each
函数返回的jQuery
对象与其调用的内容完全相同。
至于如何获得实例,让我们经历每一步:
var plugin = $('.element').plugin();
如果$('.element')
不匹配,则上面的行不会创建插件。有一点是肯定的,您分配给plugin
的值等于$('.element')
。
var instance = $('.element').data('plugin',plugin);
在上下文中,上面的行等效于var instance = $('.element').data('plugin', $('.element'));
,而jQuery.data(key, value)
的返回值是jQuery对象本身,因此这里instance
将等于$('.element')
。
console.log(instance); // an empty object again!
如果$('.element')
不匹配,则会得到这种结果。
console.log(instance.someMethod("hello world"));
这肯定是行不通的。
如何访问插件实例在代码中的注释中有详细说明:
// var instance = $('.element').data('plugin');
// instance.someMethod();
当然,为了实现这一点,$('.element')
必须匹配一组非空元素。
AMD在您的问题中根本不是一个因素。这里的所有问题都与如何使用jQuery以及如何编写jQuery插件有关。
var plugin = $('.element').plugin();
var instance = $('.element').data('plugin');
console.log(instance);
console.log(instance.someMethod("hello world"));
结果,
Object { element={...}, options={...}, constructor=function(), more...}
hello world
- 如何在Angular2中使用jQuery插件
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- 我的jQuery插件参数没有正确启动,遇到了问题
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- TableExport jquery插件:文件名和扩展名问题
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- jQuery插件-从插件中调用公共方法
- 扩展jQuery插件以更改配置
- "工具提示"jQuery插件坏了
- 不能在图像中的地图标记中使用花式框jquery插件的区域标记
- 使用Booklet Jquery插件,我如何验证最后一页是否显示
- 合并TinyMCE(jQuery插件)文件
- 带有jquery插件的backbone.js视图
- 自制jQuery插件已触发,但无法工作
- 我如何才能找到哪些网站使用我的jQuery插件
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 在多个元素上使用jquery插件,只需稍作修改
- HtmlBox(jquery插件)在ajax调用中不起作用
- 构造jQuery插件
- 使用filedownload jquery插件处理FilePathResult返回值