Mass framework data module v4

Source: Internet
Author: User

improved V3 by using two referenced arrays to provide UUID for element nodes, and the other two arrays to provide cache bodies. Delete the data class in V4. There is only one array to provide the uuid for the element node, and then an array to provide the cache body. There is a data object attribute on the cache body to save user data. Compared with the new scheme of V3 and jquery2.0, it will delete the elements in the corresponding positions of the two arrays when there is no private data and user data, to prevent infinite expansion.

// ================================================ ==============/// Data cache module ================================== ============================== define ("data ", ["$ Lang"], function ($) {var owners = [], caches = []; /*** specify a cache body for the target object * @ Param {Any} owner * @ return {object} cache body * @ API private */function add (owner) {var Index = owners. push (owner); Return caches [index-1] = {data :{}};}/ *** read/write data for the target object * @ Param {Any} owner *@ Param {object | string} Name? Data or data packet to be processed * @ Param {Any} data? Data to be written * @ Param {Boolean} PVT? Identified as internal data * @ return {Any} * @ API private */function innerdata (owner, name, Data, PVT) {// ie678 cannot add data var Index = owners to the text node comment node. indexof (owner); var table = Index =-1? Add (owner): caches [Index]; var getone = typeof name = "string" // obtain a single attribute var cache = table; // Private Data is directly put into the table, and normal data is put into the table. if (! Pvt) {table = table. data;} If (Name & typeof name = "object") {$. mix (table, name); // write a set of attributes} else if (getone & Data! = Void 0) {table [name] = data; // write a single attribute} If (getone) {If (name in Table) {return table [name];} else if (! Pvt & owner. nodetype = 1) {// for data stored using the HTML5 data-* attribute, for example, <input id = "test" data-full-name = "Planet Earth"/> // you can use $ ("# test "). data ("full-name") or $ ("# test "). data ("fullname") Access to return $. parsedata (owner, name, cache) ;}} else {return table ;}} /*** remove data from the target object * @ Param {Any} owner * @ Param {Any} Name? Data to be removed * @ Param {Boolean} PVT? Identified as internal data * @ return {Any} * @ API private */function innerremovedata (owner, name, PVT) {var Index = owners. indexof (owner); If (index>-1) {var delone = typeof name = "string", table = caches [Index], cache = table, clear = 1 If (delone) {If (! Pvt) {table = table. data;} If (table) {delone = table [name]; Delete table [name];} For (var key in cache) {If (Key = "data ") {for (var I in cache. data) {clear = 0; break ;}} else {clear = 0; break ;}} if (clear) {owners. splice (index, 1); caches. splice (index, 1) ;}} return delone; // return the removed data }}$. mix ({// determine whether the data is associated with hasdata: function (owner) {return owners. indexof (owner)>-1 ;}, // read/write user data dat A: function (target, name, data) {return innerdata (target, name, data) ;}, // read and write internal data _ DATA: function (target, name, data) {return innerdata (target, name, Data, true) ;}, // Remove User Data removedata: function (target, name) {return innerremovedata (target, name );}, // remove internal data _ removedata: function (target, name) {return innerremovedata (target, name, true );}, // convert the HTML5 data-* attribute to a richer and more useful data type and save it as parsedata: F Unction (target, name, cache, value) {var data, key = $. string. camstme (name), _ eval if (Cache & (key in cache) return cache [Key]; If (arguments. length! = 4) {var ATTR = "data-" + name. replace (/([A-Z])/g, "-$1 "). tolowercase (); value = target. getattribute (ATTR);} If (typeof value = "string") {// convert/^ (?: \ {. * \} | Null | false | true | Nan) $/If (/^ (? :\{. * \} | \[. * \] | Null | false | true | Nan) $ /. test (value) | + value + "" = value) {_ EVAL = true;} Try {DATA = _ eval? Eval ("0," + value): value;} catch (e) {DATA = value;} If (cache) {cache [Key] = data ;}} return data;}, // Merge data mergedata: function (cur, Src) {if ($. hasdata (cur) {var olddata = $. _ data (SRC), curdata = $. _ data (cur), events = olddata. events; $. object. merge (curdata, olddata); If (events) {curdata. events = []; for (VAR I = 0, item; item = events [I ++];) {$. event. BIND (cur, item) ;}}}); return $ });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.