Jquery Plugin-Metadata

Source: Internet
Author: User

Today, when reading an article-
A plugin Development Pattern, I found an interesting Plugin-
Metadata.

First, let's see it in action:

 
<Li id = "Li1" class = 'someclass {"item_id": 1, "item_label ": "label"} '> Item 1 </LI> <li id = "li2" metadata =' {"item_id": 1, "item_label ": "label"} '> Item 2 </LI> <li id = "li3"> <metadata style = "display: none;"> {"item_id": 1, "item_label": "label"} </metadata> Item 3 </LI> <li id = "LI4"> <SCRIPT type = "metadata"> {"item_id ": 1, "item_label": "label"} </SCRIPT> Item 4 </LI>
$ (Function () {$ ("# Item1 "). metadata (). item_id; // 1 $ ("# Item1 "). metadata ({"type": "class "}). item_id; // 1 $ ("# item2 "). metadata ({"type": "ATTR "}). item_label; // "label" $ ("# item2 "). metadata ({"type": "ATTR", "name": "metadata "}). item_label; // "label" $ ("# item3 "). metadata ({"type": "ELEM "}). item_label; // "label" $ ("# item3 "). metadata ({"type": "ELEM", "name": "metadata "}). item_label; // "label" $ ("# item4 "). metadata ({"type": "ELEM", "name": "script "}). item_label; // "label "});

OK, this plugin extract JSON string from class, attribute or child node and parse
It Into JavaScript Object.

Let's go through the source code:

(Function ($) {$. extend ({metadata: {defaults: {type: 'class', name: 'metadata', crere :/({. *})/, single: 'metadata'}, settype: function (type, name) {This. defaults. type = type; this. defaults. name = Name;}, get: function (ELEM, opts) {var settings = $. extend ({}, this. defaults, opts); // check for empty string in single property if (! Settings. single. length) settings. single = 'metadata'; var DATA = $. data (ELEM, settings. single); // returned cached data if it already exists if (data) return data; data = "{}"; if (settings. type = "class") {var M = settings.cre.exe C (ELEM. classname); If (m) Data = m [1];} else if (settings. type = "ELEM") {If (! ELEM. getelementsbytagname) return; var E = ELEM. getelementsbytagname (settings. name); If (E. length) Data = $. trim (E [0]. innerhtml);} else if (ELEM. getattribute! = Undefined) {var ATTR = ELEM. getattribute (settings. name); If (ATTR) Data = ATTR;} If (data. indexof ('{') <0) Data = "{" + Data + "}"; Data = eval ("(" + Data + ")"); $. data (ELEM, settings. single, data); return data ;}}); $. FN. metadata = function (OPTs) {return $. metadata. get (this [0], opts) ;}}) (jquery );

following is a modified version to demonstrate the main concept (also working well):

 (function ($) {$. FN. metadata = function (options) {var Options = $. extend ({}, {type: 'class', name: 'metadata'}, options); var DATA = This. data ("metadata"); If (data) {return data;} DATA = "{}"; if (options. type = "class") {var M = /{. *}/. exec (this. ATTR ("class"); If (m) {DATA = m [0] ;}} else if (options. type = "ELEM") {var childelem = This. find (options. name); If (childelem) {DATA = $. trim (childelem [0]. innerhtml) ;}} else if (options. type = "ATTR") {DATA = This. ATTR (options. name);} DATA = JSON. parse (data); this. data ("metadata", data); return data ;};} (jquery); 

Maybe Athena shocould take this Convention to simplify default options that passed
To widget constructor.

This is done manually by developers now. You can refer to "how to pass Parameters
To widget constructor ?" In 2.16.wbx.doc for details.

I will post another article to demonstrate how to add this feature to $. wbx client-framework later.

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.