资讯专栏INFORMATION COLUMN

让你基于jQuery的插件兼容commonjs,amd规范

GeekGhc / 2336人阅读

摘要:示例小明小明小明小明小花小花姓名价格基于上面的方法我写了个简易的基于的自动生成表格的插件,可以合并单元格。对于兼容这些规范,写法也很多,希望多多指教完整代码

事情是这样的,我写了一个基于jQuery的插件,在传统的开发模式中,我们需要现在页面引入jQuery.js,然后在引入我们的插件,我们的插件才能使用。但是随着webpack的兴起,我不在想一步步的写入script标签,写着一堆的script标签。但是我们知道这些都有着模块规范。比如在node环境中我们经常这样写:var example = require("./example.js");那么就让我们的插件也支持这样的写法吧。

先看传统式jQuery插件的写法。(jquery的插件有很多种写法,这里就不过多讨论)
;(function($,window){
   var plugin=function(){
   }

   plugin.prototype={
    a:function(){

    },
    b:function(){

    }

   }
   window.plugin=plugin;
})($,window)
这样我们在页面中这样写:
var p = new plugin()
问题来了,我都是用webpack来打包js文件的,怎么可以通过require("./plugin.js")的方式来引入我的插件呢?

node的模块规范是commonjs,这些我们是知道的,网上也有很介绍,在下就不多哔哔了。但是问题来了,网上很多只是介绍如何把一个模块导出,但是当你的模块依赖某个模块时,这时该怎么处理?

先看一段代码
(function(window,$){
        var mtable = function (opt, data) {
        this.tableId = opt.tableid;
        this.tableClass = opt.tableclass;
        this.tableParent = opt.tableparent;
        ......
        this.mergeCells = opt.mergeCells || false;
        if (this instanceof mtable) {
            this.init();
            return this;
        } else {
            return new mtable(opt, data).init()
        }
    }
    
      mtable.prototype = {
        constructor: mtable,
        init: function () {
            var that = this;
            that.createTable();
            //合并单元格
            if (this.mergeCells == true) {
                mtable.MergeCell(that.tableId)
            }
            this.addEventDom();
            this.removeEventDom();
         },
            addEventDom: function () {
            var that = this;
            //在这里我们使用了jquery
            $(this.addDom).on("click", function () {
                console.log("开始生产表格...")
                that.createTable();
            })
        }
      }
})(window,$)
接着我们尝试着在闭包函数中,写写兼容模块,让函数能被导出去
 //兼容模块
    if(typeof module !=="undefined" && typeof exports ==="object"){
        module.exports=mtable;
    }else if(typeof define ==="function" && (define.amd || define.cmd)){
        define(function(){
            return mtable;
        })
    }else{
        window.mtable=mtable;
    }

但是我们通过 var mtable = require("mtable")时,经过webpack打包,会发现览器错误提示$ is not defined
既然$ is not defined 说明我们并没把jQuery这个参数传进去。

在换种方式call()函数,对,你没看错,这个方法经常被人拿出来介绍。。。
//如果是在node环境中,通过require引入jQuery中,如果是在浏览器中,就通过window方式传递jQuery
if(typeof module !=="undefined" && typeof exports ==="object"){
    var $=require("jquery");
}else{
    var $=window.$
}
(function(){

    var mtable = function (opt, data) {
        this.tableId = opt.tableid;
        this.tableClass = opt.tableclass;
        this.tableParent = opt.tableparent;
        this.table;
        this.theade = opt.theade;
        this.addDom = opt.affffdom;
        this.removeDom = opt.removedom;
        this.data = data;
        this.mergeCells = opt.mergeCells || false;
        if (this instanceof mtable) {
            this.init();
            return this;
        } else {
            return new mtable(opt, data).init()
        }
    }
    mtable.prototype = {
        constructor: mtable,
        init: function () {
            var that = this;
            that.createTable();
            //合并单元格
            if (this.mergeCells == true) {
                mtable.MergeCell(that.tableId)
            }
            this.addEventDom();
            this.removeEventDom();
            return this;
        },
        createTable: function () {
            var that = this;
            var t = createTable({
                id: that.tableId,
                class: that.tableClass
            }, that.data, that.theade);
            if (this.tableParent != null && this.tableParent != "") {
                $(this.tableParent).append(t)
            } else {
                document.body.appendChild(t);
            }
        },
      
        addEventDom: function () {
            var that = this;
            //var tableObj=document.getElementById("m");
            $(this.addDom).on("click", function () {
                console.log("开始生产表格...")
                that.createTable();
            })
        },
        removeEventDom: function () {
            $(this.removeDom).on("click", function () {

            })
        }
    }
   
   
      
        $.each(data, function (index, item) {
            var tr = document.createElement("tr");
            for (var i in item) {
                console.log(item)
                var td = document.createElement("td");
                td.innerHTML = item[i];
                tr.appendChild(td);
            }
            tbody.appendChild(tr);
        });
        vtable.appendChild(tbody);
        return vtable;
    }

    }
    //兼容模块
    if(typeof module !=="undefined" && typeof exports ==="object"){
        module.exports=mtable;
    }else if(typeof define ==="function" && (define.amd || define.cmd)){
        define(function(){
            return mtable;
        })
    }else{
        window.mtable=mtable;
    }
}).call(function(){
   return (typeof window !=="undefined" ? window : global )
},$)//传入$

不论是在node中,还是在浏览器中都可以引用了。

示例
   
    
var mtable=require("../lib/easyTable");
var data = [
    { "a": "小明", "b": "100" },
    { "a": "小明", "b": "250" },
    { "a": "小明", "b": "260" },
    { "a": "小明", "b": "270" },
    { "a": "小花", "b": "90" },
    { "a": "小花", "b": "190" }
]
var c = new mtable({
    tableid: "m",
    affffdom: ".add",
    tableclass: "table table-bordered",
    tableparent: ".tcon",
    theade: "姓名价格",
    mergeCells: true
}, data)

基于上面的方法我写了个简易的基于jQuery的自动生成表格的插件,可以合并单元格。对于兼容commonjs这些规范,写法也很多,希望多多指教
完整代码github:easyTable

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/82872.html

相关文章

  • OMD: javascript模块化开发兼容CommonJS, AMD, CMD 以及 原生 JS

    摘要:它就是一套兼容方案,目前兼容的有以及原生支持。返回值问题在第一次使用时,。具体是什么意义呢的返回值,其实就是插件提供的对外接口,而实际上,就是一个对象。而在环境下,只需要将这个返回值赋予即可完成该模块的接口。 有更新,请到github上看源码 什么是OMD 在node.js流行起来之前,javascript的开发方式都是函数式的顺序依赖关系,直到node火起来。CommonJS其实首先...

    lavor 评论0 收藏0
  • 「JavaScript」如何让你插件兼容CommonJS, AMD, CMD 和 原生 JS

    摘要:模块标准有三个全局变量和。模块中有两种方式提供对外的接口,一种是,一种是使用进行返回。规范中,函数同样有一个公有属性。由于和都可以使用来定义对外接口,故可以合并成一句代码。 模块标准 CommonJS CommonJS 有三个全局变量 module、exports 和 require。但是由于 AMD 也有 require 这个全局变量,故不使用这个变量来进行检测。 如果想要对外提供接...

    ShevaKuilin 评论0 收藏0
  • 也谈 webpack 及其开发模式

    摘要:比如通过安装实例一新建一个然后编辑加入打开浏览器,看到屏幕输出会给每个模块一个唯一的然后通过存取这些模块,这些模块都会被整合到里面。以上设置会输出一个的文件。 从模块化谈起 近年来,js开发涌现出了诸多模块化解决方案,例如以在浏览器环境之外(服务端)构建 JavaScript 生态系统为目标而产生的CommonJS规范,从CommonJS社区中独立出来的AMD规范(异步模块定义),还有...

    huhud 评论0 收藏0
  • JS模块化编程

    摘要:也就是说,外部模块输出值变了,当前模块的导入值不会发生变化。三规范的出现,使得模块化在环境中得到了施展机会。模块化这种加载称为编译时加载或者静态加载。总结的模块化规范经过了模块模式的演进,利用现在常用的打包工具,非常方便我们编写模块化代码。 前言 什么是模块化? 模块就是实现特定功能的一组方法,而模块化是将模块的代码创造自己的作用域,只向外部暴露公开的方法和变量,而这些方法之间高度解耦...

    骞讳护 评论0 收藏0
  • 再谈JavaScript模块化

    摘要:应用日益复杂,模块化已经成为一个迫切需求。异步模块加载机制。引用的资源列表太长,懒得回调函数中写一一对应的相关参数假定这里引用的资源有数十个,回调函数的参数必定非常多这就是传说中的 简述 缘起 模块通常是指编程语言所提供的代码组织机制,利用此机制可将程序拆解为独立且通用的代码单元。 模块化主要是解决代码分割、作用域隔离、模块之间的依赖管理以及发布到生产环境时的自动化打包与处理等多个方面...

    MorePainMoreGain 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<