本篇介绍directive的简单实现,主要学习其实现的思路及代码的设计,需要的朋友参考下吧
2018年首个计划是学习vue源码,查阅了一番资料之后,决定从第一个commit开始看起,这将是一场持久战!本篇介绍directive的简单实现,主要学习其实现的思路及代码的设计(directive和filter扩展起来非常方便,符合设计模式中的 开闭原则 )。
构思API
<p id="app" sd-on-click="toggle | .button"> <p sd-text="msg | capitalize"></p> <p sd-class-red="error" sd-text="content"></p> <button class="button">Toggle class</button> </p> var app = Seed.create({ id: 'app', scope: { msg: 'hello', content: 'world', error: true, toggle: function() { app.scope.error = !app.scope.error; } } });
实现功能够简单吧--将scope中的数据绑定到app中。
核心逻辑设计
指令格式
以 sd-text="msg | capitalize" 为例说明:
sd 为统一的前缀标识
text 为指令名称
capitalize 为过滤器名称
其中 | 后面为过滤器,可以添加多个。 sd-class-red
中的red为参数。
代码结构介绍
main.js 入口文件
// Seed构造函数 const Seed = function(opts) { }; // 对外暴露的API module.exports = { create: function(opts) { return new Seed(opts); } }; directives.js module.exports = { text: function(el, value) { el.textContent = value || ''; } }; filters.js module.exports = { capitalize: function(value) { value = value.toString(); return value.charAt(0).toUpperCase() + value.slice(1); } };
就这三个文件,其中directives和filters都是配置文件,很易于扩展。
实现的大致思路如下:
1.在Seed实例创建的时候会依次解析el容器中node节点的指令
2.将指令解析结果封装为指令对象,结构为:
属性 | 说明 | 类型 |
---|---|---|
attr | 原始属性,如sd-text
|
String |
key | 对应scope对象中的属性名称 | String |
filters | 过滤器名称列表 | Array |
definition | 该指令的定义,如text对应的函数 | Function |
argument | 从attr中解析出来的参数(只支持一个参数) | String |
update | 更新directive时调用typeof def === 'function' ? def : def.update
|
Function |
bind | 如果directive中定义了bind方法,则在bindDirective 中会调用 |
Function |
el | 存储当前element元素 | Element |
3.想办法执行指令的update方法即可,该插件使用了 Object.defineProperty 来定义scope中的每个属性,在其setter中触发指令的update方法。
核心代码
const prefix = 'sd'; const Directives = require('./directives'); const Filters = require('./filters'); // 结果为[sd-text], [sd-class], [sd-on]的字符串 const selector = Object.keys(Directives).map((name) => `[${prefix}-${name}]`).join(','); const Seed = function(opts) { const self = this, root = this.el = document.getElementById(opts.id), // 筛选出el下所能支持的directive的nodes列表 els = this.el.querySelectorAll(selector), bindings = {}; this.scope = {}; // 解析节点 [].forEach.call(els, processNode); // 解析根节点 processNode(root); // 给scope赋值,触发setter方法,此时会调用与其相对应的directive的update方法 Object.keys(bindings).forEach((key) => { this.scope[key] = opts.scope[key]; }); function processNode(el) { cloneAttributes(el.attributes).forEach((attr) => { const directive = parseDirective(attr); if (directive) { bindDirective(self, el, bindings, directive); } }); } };
可以看到核心方法 processNode 主要做了两件事一个是 parseDirective ,另一个是 bindDirective 。
先来看看 parseDirective 方法:
function parseDirective(attr) { if (attr.name.indexOf(prefix) == -1) return; // 解析属性名称获取directive const noprefix = attr.name.slice(prefix.length + 1), argIndex = noprefix.indexOf('-'), dirname = argIndex === -1 ? noprefix : noprefix.slice(0, argIndex), arg = argIndex === -1 ? null : noprefix.slice(argIndex + 1), def = Directives[dirname] // 解析属性值获取filters const exp = attr.value, pipeIndex = exp.indexOf('|'), key = (pipeIndex === -1 ? exp : exp.slice(0, pipeIndex)).trim(), filters = pipeIndex === -1 ? null : exp.slice(pipeIndex + 1).split('|').map((filterName) => filterName.trim()); return def ? { attr: attr, key: key, filters: filters, argument: arg, definition: Directives[dirname], update: typeof def === 'function' ? def : def.update } : null; }
以 sd-on-click="toggle | .button" 为例来说明,其中attr对象的name为 sd-on-click ,value为 toggle | .button ,最终解析结果为:
{ "attr": attr, "key": "toggle", "filters": [".button"], "argument": "click", "definition": {"on": {}}, "update": function(){} }
紧接着调用 bindDirective 方法
/** * 数据绑定 * @param {Seed} seed Seed实例对象 * @param {Element} el 当前node节点 * @param {Object} bindings 数据绑定存储对象 * @param {Object} directive 指令解析结果 */ function bindDirective(seed, el, bindings, directive) { // 移除指令属性 el.removeAttribute(directive.attr.name); // 数据属性 const key = directive.key; let binding = bindings[key]; if (!binding) { bindings[key] = binding = { value: undefined, directives: [] // 与该数据相关的指令 }; } directive.el = el; binding.directives.push(directive); if (!seed.scope.hasOwnProperty(key)) { bindAccessors(seed, key, binding); } } /** * 重写scope西乡属性的getter和setter * @param {Seed} seed Seed实例 * @param {String} key 对象属性即opts.scope中的属性 * @param {Object} binding 数据绑定关系对象 */ function bindAccessors(seed, key, binding) { Object.defineProperty(seed.scope, key, { get: function() { return binding.value; }, set: function(value) { binding.value = value; // 触发directive binding.directives.forEach((directive) => { // 如果有过滤器则先执行过滤器 if (typeof value !== 'undefined' && directive.filters) { value = applyFilters(value, directive); } // 调用update方法 directive.update(directive.el, value, directive.argument, directive); }); } }); } /** * 调用filters依次处理value * @param {任意类型} value 数据值 * @param {Object} directive 解析出来的指令对象 */ function applyFilters(value, directive) { if (directive.definition.customFilter) { return directive.definition.customFilter(value, directive.filters); } else { directive.filters.forEach((name) => { if (Filters[name]) { value = Filters[name](value); } }); return value; } }
其中的bindings存放了数据和指令的关系,该对象中的key为opts.scope中的属性,value为Object,如下:
{ "msg": { "value": undefined, "directives": [] // 上面介绍的directive对象 } }
数据与directive建立好关系之后, bindAccessors 中为seed的scope对象的属性重新定义了getter和setter,其中setter会调用指令update方法,到此就已经完事具备了。
Seed构造函数在实例化的最后会迭代bindings中的key,然后从opts.scope找到对应的value, 赋值给了scope对象,此时setter中的update就触发执行了。
下面再看一下 sd-on 指令的定义:
{ on: { update: function(el, handler, event, directive) { if (!directive.handlers) { directive.handlers = {}; } const handlers = directive.handlers; if (handlers[event]) { el.removeEventListener(event, handlers[event]); } if (handler) { handler = handler.bind(el); el.addEventListener(event, handler); handlers[event] = handler; } }, customFilter: function(handler, selectors) { return function(e) { const match = selectors.every((selector) => e.target.matches(selector)); if (match) { handler.apply(this, arguments); } } } } }
发现它有customFilter,其实在 applyFilters 中就是针对该指令做的一个单独的判断,其中的selectors就是[".button"],最终返回一个匿名函数(事件监听函数),该匿名函数当做value传递给update方法,被其handler接收,update方法处理的是事件的绑定。这里其实实现的是事件的代理功能,customFilter中将handler包装一层作为事件的监听函数,同时还实现事件代理功能,设计的比较巧妙!
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
以上是在vue中如何实现directive功能的详细内容。更多信息请关注PHP中文网其他相关文章!

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务

本文展示了与许可证确保的后端的前端集成,并使用Next.js构建功能性Edtech SaaS应用程序。 前端获取用户权限以控制UI的可见性并确保API要求遵守角色库

JavaScript是现代Web开发的核心语言,因其多样性和灵活性而广泛应用。1)前端开发:通过DOM操作和现代框架(如React、Vue.js、Angular)构建动态网页和单页面应用。2)服务器端开发:Node.js利用非阻塞I/O模型处理高并发和实时应用。3)移动和桌面应用开发:通过ReactNative和Electron实现跨平台开发,提高开发效率。

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器