Skip to content

Instantly share code, notes, and snippets.

Created April 11, 2014 01:43
Show Gist options
  • Save anonymous/10436595 to your computer and use it in GitHub Desktop.
Save anonymous/10436595 to your computer and use it in GitHub Desktop.
Viewloader rewrite
var views = {
foo: function($el, el, props) {
console.log(props);
},
bar: function($el, el, props) {
console.log(props);
}
};
viewloader.execute(views);
// -> bar
// -> {foo: "bar"}
// -> {bar: "baz"}
// -> {bar: "foo"}
window.viewloader = {
execute: function(views, $scope) {
for(var view in views) {
var selector = "[data-view-" + view + "]";
$els = $scope ? $scope.find(selector) : $(selector);
$els.each(function(i, el) {
var $el = $(el);
views[view]($el, el, $el.data("view-" + view));
});
};
}
};
<div data-view-foo="bar" data-view-bar='{"bar":"baz"}'/>
<div data-view-foo='{"foo":"bar"}'/>
<div data-view-bar='{"bar":"foo"}'/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment