Created
August 30, 2012 18:44
-
-
Save jkappers/3537204 to your computer and use it in GitHub Desktop.
Attach instances of javascript classes to DOM nodes
This file contains 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
(function(exports){ | |
// When the DOM is ready... | |
$(function(){ | |
// ...Find any elements that have a data-view attribute | |
$('[data-view]').each(function(){ | |
// Store a reference to the node and the data-view attribute value. | |
var $node = $(this), attr = $node.data('view'); | |
// Create an instance of a class with a name that matches the value of attr, | |
// pass the node as a parameter to the constructor, and then store the instance | |
// in the node's data hash. | |
$node.data(attr, new exports[attr]($node)); | |
}); | |
}); | |
})(window); // Instead of window, use an object that contains your view definitions. |
This file contains 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
<script> | |
function AnimalView($element){ | |
this.$element = $element; | |
} | |
</script> | |
<div data-view="AnimalView"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Word!