Skip to content

Commit

Permalink
Rewrite in proper factory format as per v2 & bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Smithett committed Oct 15, 2014
1 parent 0f95a5c commit da1086c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bower_components
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add `data-view` attributes to your HTML:
Create an object for your app that lists setup functions for each type of view.

```javascript
myApp.views = {
MyApp.Views = {
dropdown: function( $el ) { $el.fancyDropdown(); },
chatWindow: function( $el, el ) { new ChatWindowView({ el: el }); },
// ... etc etc
Expand All @@ -23,10 +23,12 @@ myApp.views = {
Once the DOM is ready, run:

```javascript
viewloader.execute( myApp.views );
viewloader.execute( MyApp.Views );
```

viewloader will find every element on the page with a `data-view` attribute and call its setup function with 2 arguments:
viewloader will find every element on the page with a `data-view` attribute and check to see if a function with that name exists on the supplied object.

If such a function exists, it will be called 2 arguments:

- `$el`: the jQuery-wrapped DOM element (i.e. `$(el)`)
- `el`: the DOM element
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "viewloader",
"main": "viewloader.js",
"version": "1.0.0",
"version": "1.0.1",
"homepage": "https://github.com/bensmithett/viewloader",
"authors": [
"Ben Smithett <[email protected]>"
Expand All @@ -14,6 +14,7 @@
"ignore": [
"**/.*",
"README.md",
"test.html"
"test.html",
".gitignore"
]
}
5 changes: 3 additions & 2 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

<div id="test-results"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="viewloader.js"></script>

<script>
var ids = "";
var results = document.getElementById( "test-results" );

// When an object is supplied as the first argument
// It calls setup functions from that object
viewloader.execute({
single: function ( $el, el ) {
$el.addClass("single-ready");
Expand Down
28 changes: 17 additions & 11 deletions viewloader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
window.viewloader = {
execute: function( views, $scope ) {
var $els = $scope ? $scope.find( "[data-view]" ) : $( "[data-view]" );

$els.each( function( i, el ) {
var $el = $( el );
var view = $el.data( "view" );
if ( view && views[ view ] ) {
views[ view ]( $el, el );
(function (root, factory) {
root.viewloader = factory({}, (root.jQuery || root.Zepto || root.$), root);
}(this, function(viewloader, $, root) {
"use strict";

viewloader.execute = function (views, $scope) {
var $els = $scope ? $scope.find("[data-view]") : $("[data-view]");

$els.each(function(i, el) {
var $el = $(el);
var view = $el.data("view");
if (view && views[view]) {
views[view]($el, el);
}
});
}
};
};

return viewloader;
}));

0 comments on commit da1086c

Please sign in to comment.