jquery插件select2源码解读(三) options
来源:互联网 发布:cf显示客户端数据异常 编辑:程序博客网 时间:2024/06/11 23:32
在这一小节,我们开始详细的阐述select2插件是怎么从初始化到生成的。
看代码:
define([ 'jquery', './options', './utils', './keys'], function ($, Options, Utils, KEYS) {//....})使用过requirejs的都知道define方法的使用。从上面的代码可以看出,该方法把依赖的模块jquery,options,utils,keys注入到下面的方法中,并且模块和参数是一一对应关系,并且顺序一致。jquery想必大家都很熟悉,那么后面三个模块我们看代码吧^^.
先看看整体结构:
var Select2 = function ($element, options) {//初始化插件 if ($element.data('select2') != null) { $element.data('select2').destroy(); }//code…$element.data('select2', this); };Utils.Extend(Select2, Utils.Observable);//观察者模式Select2.prototype._generateId = function ($element) { };Select2.prototype._placeContainer = function ($container) { };//code select2原型链方法//…//… Select2.prototype.destroy = function () {};Select2.prototype.render = function () {};return Select2;//最后生成了select2下面来细看整个生成过程:
if ($element.data('select2') != null) { $element.data('select2').destroy(); } this.$element = $element; this.id = this._generateId($element);当我们定义的select控件元素属性中定义了data-select2,那么就销毁已生成的插件。
Select2.prototype._generateId = function ($element) { var id = ''; if ($element.attr('id') != null) { id = $element.attr('id'); } else if ($element.attr('name') != null) { id = $element.attr('name') + '-' + Utils.generateChars(2); } else { id = Utils.generateChars(4); } id = id.replace(/(:|\.|\[|\]|,)/g, ''); id = 'select2-' + id; return id; };这里this.id生成由'select2-'和id拼接的标识。接下来可以看到:
options = options || {}; this.options = new Options(options, $element);这里初始化了options的信息,new了一下,看一下options这个依赖模块的:
define([ 'require', 'jquery', './defaults', './utils'], function (require, $, Defaults, Utils) { function Options (options, $element) {} Options.prototype.fromElement = function ($e) {}; Options.prototype.get = function (key) {}; Options.prototype.set = function (key, val) {};return Options;}上面采用了同样的注入方式。这里主要看defaults和untils模块。那么先看options的初始化吧:(继续copy代码^_^~)
function Options (options, $element) { this.options = options; if ($element != null) { this.fromElement($element); } this.options = Defaults.apply(this.options); if ($element && $element.is('input')) { var InputCompat = require(this.get('amdBase') + 'compat/inputData'); this.options.dataAdapter = Utils.Decorate( this.options.dataAdapter, InputCompat ); } }我们直接跳到this.options = Defaults.apply(this.options);于是我们就看到了select2默认的参数,依赖模块defaults:
Defaults.prototype.apply = function (options) { options = $.extend(true, {}, this.defaults, options); if (options.dataAdapter == null) { if (options.ajax != null) { options.dataAdapter = AjaxData; } else if (options.data != null) { options.dataAdapter = ArrayData; } else { options.dataAdapter = SelectData; }//.....return options; };这里把所有的依赖的模块以define的方式注入到defaults的options中
define([ 'jquery', 'require', './results', './selection/single', './selection/multiple', './selection/placeholder', './selection/allowClear', './selection/search', './selection/eventRelay', './utils', './translation', './diacritics', './data/select', './data/array', './data/ajax', './data/tags', './data/tokenizer', './data/minimumInputLength', './data/maximumInputLength', './data/maximumSelectionLength', './dropdown', './dropdown/search', './dropdown/hidePlaceholder', './dropdown/infiniteScroll', './dropdown/attachBody', './dropdown/minimumResultsForSearch', './dropdown/selectOnClose', './dropdown/closeOnSelect', './i18n/en']
简单的看下有我们常用的单选下拉,多选下拉,placeholder,ajax...etc...
这样options就初始化完成了。
阅读全文
0 0
- jquery插件select2源码解读(三) options
- jquery插件select2源码解读(一) 概述
- jquery插件select2源码解读(二) 流程
- JQuery插件之select2
- JQUERY插件select2.js
- jquery select2插件 改造
- Tornado.options源码解读
- jquery select2插件初始化时赋多个值
- jquery select2插件初始化时赋多个值
- Select2插件 点击、选中事件 解读
- jquery插件select2事件不起作用(select2-3.5.4)
- jQuery的下拉选select2插件用法
- jQuery插件记录 <三>使用$.fn添加jQuery对象方法中传递options给定默认值
- jquery.validate.js校验select2解决方案 Jquery插件select2校验解决方案
- jQuery support 源码解读
- jQuery support 源码解读
- jQuery源码解读一
- jQuery源码解读二
- 名企笔试:Microsoft笔试题(URLify a given string )
- Spring中使用Redis
- Java泛型详解
- MySQL/InnoDB处理AUTO_INCREMENT(一)
- 注解的实例
- jquery插件select2源码解读(三) options
- Linux C 程序内存管理【笔记】
- Android+JNI+OpenGL开发自己的美图秀秀
- ubuntu16.04 安装搜狗输入法
- 深入理解javascript原型和闭包(8)——简述【执行上下文】
- c++复数类
- 鸟哥Linux学习之——设置bash环境
- 完全二部图匹配
- 关于实习