摘要:示例小明小明小明小明小花小花姓名價(jià)格基于上面的方法我寫了個(gè)簡易的基于的自動(dòng)生成表格的插件,可以合并單元格。對于兼容這些規(guī)范,寫法也很多,希望多多指教完整代碼
先看傳統(tǒng)式j(luò)Query插件的寫法。(jquery的插件有很多種寫法,這里就不過多討論)事情是這樣的,我寫了一個(gè)基于jQuery的插件,在傳統(tǒng)的開發(fā)模式中,我們需要現(xiàn)在頁面引入jQuery.js,然后在引入我們的插件,我們的插件才能使用。但是隨著webpack的興起,我不在想一步步的寫入script標(biāo)簽,寫著一堆的script標(biāo)簽。但是我們知道這些都有著模塊規(guī)范。比如在node環(huán)境中我們經(jīng)常這樣寫:var example = require("./example.js");那么就讓我們的插件也支持這樣的寫法吧。
;(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的模塊規(guī)范是commonjs,這些我們是知道的,網(wǎng)上也有很介紹,在下就不多嗶嗶了。但是問題來了,網(wǎng)上很多只是介紹如何把一個(gè)模塊導(dǎo)出,但是當(dāng)你的模塊依賴某個(gè)模塊時(shí),這時(shí)該怎么處理?
(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("開始生產(chǎn)表格...") 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")時(shí),經(jīng)過webpack打包,會發(fā)現(xiàn)覽器錯(cuò)誤提示$ is not defined
既然$ is not defined 說明我們并沒把jQuery這個(gè)參數(shù)傳進(jìn)去。
//如果是在node環(huán)境中,通過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("開始生產(chǎn)表格...") 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: "姓名 價(jià)格 ", mergeCells: true }, data)
基于上面的方法我寫了個(gè)簡易的基于jQuery的自動(dòng)生成表格的插件,可以合并單元格。對于兼容commonjs這些規(guī)范,寫法也很多,希望多多指教
完整代碼github:easyTable
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/82872.html
摘要:它就是一套兼容方案,目前兼容的有以及原生支持。返回值問題在第一次使用時(shí),。具體是什么意義呢的返回值,其實(shí)就是插件提供的對外接口,而實(shí)際上,就是一個(gè)對象。而在環(huán)境下,只需要將這個(gè)返回值賦予即可完成該模塊的接口。 有更新,請到github上看源碼 什么是OMD 在node.js流行起來之前,javascript的開發(fā)方式都是函數(shù)式的順序依賴關(guān)系,直到node火起來。CommonJS其實(shí)首先...
摘要:模塊標(biāo)準(zhǔn)有三個(gè)全局變量和。模塊中有兩種方式提供對外的接口,一種是,一種是使用進(jìn)行返回。規(guī)范中,函數(shù)同樣有一個(gè)公有屬性。由于和都可以使用來定義對外接口,故可以合并成一句代碼。 模塊標(biāo)準(zhǔn) CommonJS CommonJS 有三個(gè)全局變量 module、exports 和 require。但是由于 AMD 也有 require 這個(gè)全局變量,故不使用這個(gè)變量來進(jìn)行檢測。 如果想要對外提供接...
摘要:比如通過安裝實(shí)例一新建一個(gè)然后編輯加入打開瀏覽器,看到屏幕輸出會給每個(gè)模塊一個(gè)唯一的然后通過存取這些模塊,這些模塊都會被整合到里面。以上設(shè)置會輸出一個(gè)的文件。 從模塊化談起 近年來,js開發(fā)涌現(xiàn)出了諸多模塊化解決方案,例如以在瀏覽器環(huán)境之外(服務(wù)端)構(gòu)建 JavaScript 生態(tài)系統(tǒng)為目標(biāo)而產(chǎn)生的CommonJS規(guī)范,從CommonJS社區(qū)中獨(dú)立出來的AMD規(guī)范(異步模塊定義),還有...
摘要:也就是說,外部模塊輸出值變了,當(dāng)前模塊的導(dǎo)入值不會發(fā)生變化。三規(guī)范的出現(xiàn),使得模塊化在環(huán)境中得到了施展機(jī)會。模塊化這種加載稱為編譯時(shí)加載或者靜態(tài)加載??偨Y(jié)的模塊化規(guī)范經(jīng)過了模塊模式的演進(jìn),利用現(xiàn)在常用的打包工具,非常方便我們編寫模塊化代碼。 前言 什么是模塊化? 模塊就是實(shí)現(xiàn)特定功能的一組方法,而模塊化是將模塊的代碼創(chuàng)造自己的作用域,只向外部暴露公開的方法和變量,而這些方法之間高度解耦...
摘要:應(yīng)用日益復(fù)雜,模塊化已經(jīng)成為一個(gè)迫切需求。異步模塊加載機(jī)制。引用的資源列表太長,懶得回調(diào)函數(shù)中寫一一對應(yīng)的相關(guān)參數(shù)假定這里引用的資源有數(shù)十個(gè),回調(diào)函數(shù)的參數(shù)必定非常多這就是傳說中的 簡述 緣起 模塊通常是指編程語言所提供的代碼組織機(jī)制,利用此機(jī)制可將程序拆解為獨(dú)立且通用的代碼單元。 模塊化主要是解決代碼分割、作用域隔離、模塊之間的依賴管理以及發(fā)布到生產(chǎn)環(huán)境時(shí)的自動(dòng)化打包與處理等多個(gè)方面...
閱讀 1135·2021-10-27 14:15
閱讀 2843·2021-10-25 09:45
閱讀 2026·2021-09-02 09:45
閱讀 3414·2019-08-30 15:55
閱讀 1884·2019-08-29 16:05
閱讀 3292·2019-08-28 18:13
閱讀 3159·2019-08-26 13:58
閱讀 503·2019-08-26 12:01