Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit c7ef911

Browse files
author
joern
committed
A few more improvements: single: "data" is now the default to store data. Docs simplified and more useful.
git-svn-id: http://jqueryjs.googlecode.com/svn/trunk/plugins/metadata@749 c715fcbe-d12f-0410-84c4-316a508785bb
1 parent 7392e8d commit c7ef911

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

metadata.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
1+
/*
2+
* Metadata - jQuery plugin for parsing metadata from elements
3+
*
4+
* Copyright (c) 2006 John Resig, Yehuda Katz, Jörn Zaefferer
5+
*
6+
* Dual licensed under the MIT and GPL licenses:
7+
* http://www.opensource.org/licenses/mit-license.php
8+
* http://www.gnu.org/licenses/gpl.html
9+
*
10+
* Revision: $Id$
11+
*
12+
*/
13+
114
/**
2-
* Sets the type of metadata encoding to use. Metadata is encoded in JSON, and each property
15+
* Sets the type of metadata to use. Metadata is encoded in JSON, and each property
316
* in the JSON will become a property of the element itself.
417
*
5-
* Requires jQuery core 1.0.4+!
6-
*
7-
* There are three encoding types:
18+
* There are three supported types of metadata storage:
819
*
9-
* attr: The data will be stored in an attribute. The second parameter of $.meta.setType
10-
* will indicate *which* attribute.
20+
* attr: Inside an attribute. The name parameter indicates *which* attribute.
1121
*
12-
* class: The data will be stored in the class, inside { } (default)
22+
* class: Inside the class attribute, wrapped in curly braces: { }
1323
*
14-
* elem: Store the data in an element inside the current element (e.g. a script tag). The
15-
* second parameter of $.meta.setType will indicate *which* element
24+
* elem: Inside a child element (e.g. a script tag). The
25+
* name parameter indicates *which* element.
1626
*
17-
* The metadata for an element is loaded the first time the element is loaded via a jQuery query.
27+
* The metadata for an element is loaded the first time the element is accessed via jQuery.
28+
*
1829
* As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
1930
* matched by expr, then redefine the metadata type and run another $(expr) for other elements.
2031
*
2132
* @name $.meta.setType
33+
*
2234
* @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
2335
* @before $.meta.setType("class")
24-
* @after $("#one")[0].item_id == 1; $("#one")[0].item_label == "Label"
36+
* @after $("#one").data().item_id == 1; $("#one")[0].item_label == "Label"
37+
* @desc Reads metadata from the class attribute
2538
*
2639
* @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
2740
* @before $.meta.setType("attr", "data")
28-
* @after $("#one")[0].item_id == 1; $("#one")[0].item_label == "Label"
41+
* @after $("#one").data().item_id == 1; $("#one")[0].item_label == "Label"
42+
* @desc Reads metadata from a "data" attribute
2943
*
3044
* @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
3145
* @before $.meta.setType("elem", "script")
32-
* @after $("#one")[0].item_id == 1; $("#one")[0].item_label == "Label"
46+
* @after $("#one").data().item_id == 1; $("#one")[0].item_label == "Label"
47+
* @desc Reads metadata from a nested script element
3348
*
3449
* @param String type The encoding type
3550
* @param String name The name of the attribute to be used to get metadata (optional)
36-
* @author John Resig
3751
* @cat Plugins/Metadata
3852
* @descr Sets the type of encoding to be used when loading metadata for the first time
3953
* @type undefined
40-
*
41-
* Revision: $Id$
54+
* @see data()
4255
*/
4356

4457
(function($) {
@@ -51,7 +64,7 @@
5164
this.name = name;
5265
},
5366
cre: /({.*})/,
54-
single: ''
67+
single: 'data'
5568
};
5669

5770
// reference to original set()
@@ -96,11 +109,11 @@
96109
* Returns the metadata object for the first member of the jQuery object.
97110
*
98111
* @name data
99-
* @descr Return's element's metadata object
112+
* @descr Returns element's metadata object
100113
* @type jQuery
101114
* @cat Plugins/Metadata
102115
*/
103116
$.fn.data = function(){
104-
return this[0][$.meta.name];
117+
return this[0][$.meta.single || "data"];
105118
};
106119
})(jQuery);

metadataTest.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<link rel="stylesheet" href="../../jquery/build/test/data/testsuite.css" />
55
<script type="text/javascript" src="../../jquery/dist/jquery.js"></script>
66
<script type="text/javascript" src="../../jquery/build/test/data/testrunner.js"></script>
7+
<!-- include metadata plugin two times to make sure that doesn't hurt anyone -->
78
<script type="text/javascript" src="metadata.js"></script>
89
<script type="text/javascript" src="metadata.js"></script>
910
<script type="text/javascript">
@@ -79,9 +80,9 @@
7980
expect(3);
8081
$.meta.setType("class");
8182
$.meta.single = "stuff";
82-
var e = $('#meal')[0];
83-
ok( e.stuff );
84-
ok( e.stuff.required );
83+
var e = $('#meal').data();
84+
ok( e, "data property" );
85+
ok( e.required, "property on data property" );
8586
});
8687
</script>
8788
</head>

0 commit comments

Comments
 (0)