Created
March 1, 2010 14:55
-
-
Save jonDowdle/318434 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
| "http://www.w3.org/TR/html4/loose.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| <title>New Web Project</title> | |
| <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js""></script> | |
| <script type="text/javascript"> | |
| $(function(){ | |
| // Begin overriding jQuery data() | |
| var originalData = jQuery.fn.data; | |
| jQuery.fn.data = function(){ | |
| if (!arguments.length) { | |
| var dataStore = originalData.apply(this); | |
| if (dataStore) { // when no arguments and no data stored | |
| return (dataStore); | |
| } | |
| originalData.apply(this, ['_','_']); | |
| return (originalData.apply(this)); | |
| }else{ | |
| // If we are stashing data trigger event | |
| if(arguments.length == 2){ | |
| var returned = originalData.apply(this, arguments); | |
| $(this).trigger('DATA_CHANGE', arguments[0]); | |
| return (returned); | |
| } | |
| return (originalData.apply(this, arguments)); | |
| } | |
| }; | |
| // END overriding jQuery data(); | |
| container = $("#main-container"); | |
| function loadData(el, key, data ){ | |
| el.data(key, data); | |
| } | |
| function printRecord(data){ | |
| return '<li>Name: ' + data.name + '</li>' + | |
| '<li>Age: ' + data.age + '</li>'; | |
| } | |
| $("#load-1").click(function(){ | |
| container.data('person', {name:'Jon',age:26}); | |
| }); | |
| $("#load-2").click(function(){ | |
| container.data('person-2', {name:'Emily',age:25}); | |
| }); | |
| container.bind('DATA_CHANGE',function(event,key){ | |
| var target = event.target; | |
| console.log('In DATA_CHANGE', arguments); | |
| console.log($(target).data(key)); | |
| $(target).append( printRecord( $(target).data(key) ) ); | |
| }); | |
| }) | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Item Renderer in jQuery</h1> | |
| <input id="load-1" type="button" value="Load data set 1" /> | |
| <input id="load-2" type="button" value="Load data set 2" /> | |
| <ul id="main-container"></ul> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment