-
-
Save kolber/10610326 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
var views = { | |
foo: function($el, el, props) { | |
console.log(props); | |
}, | |
bar: function($el, el, props) { | |
console.log(props); | |
}, | |
fooBar: function($el, el, props) { | |
console.log(props); | |
} | |
}; | |
viewloader.execute(views); | |
// -> "bar" | |
// -> {"bar": "baz"} | |
// -> {"bar": "foo"} | |
// -> {"fooBar": true} |
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
(function(root, factory) { | |
root.viewloader = factory({},(root.jQuery || root.Zepto || root.$)); // Browser global | |
}(this, function(viewloader,$) { | |
"use strict"; | |
var re = new RegExp('([a-z])([A-Z])', 'g'), | |
dasherize = function(s) { | |
return s.replace(re, '$1-$2').toLowerCase(); | |
}; | |
viewloader.execute = function(views, $scope) { | |
for(var view in views) { | |
var dashView = dasherize(view), | |
selector = "[data-view-" + dashView + "]", | |
$els = $scope ? $scope.find(selector) : $(selector); | |
$els.each(function(i, el) { | |
var $el = $(el); | |
views[view]($el, el, $el.data("view-" + dashView)); | |
}); | |
}; | |
}; | |
return viewloader; | |
})); |
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
<div data-view-foo="bar" data-view-bar='{"bar":"baz"}'/> | |
<div data-view-bar='{"bar":"foo"}'/> | |
<div data-view-foo-bar='{"fooBar":true}'/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment