Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Finn-Lennart Heemeyer committed Oct 29, 2014
1 parent f0c86c1 commit a3b23f6
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_STORE
71 changes: 71 additions & 0 deletions ICanHaz.load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
(function(window, document, $) {

/*
* ICanHaz.load.js
*
* Usage:
*
* ich.load('template', function() {
* // Executed as soon as template loading has finished
* var $tpl = ich.template({
* foo: 'bar'
* });
* });
*
*
* by Finn-Lennart Heemeyer, License: MIT
*/

var ich = window.ich,
templates = {};

ich.load = function(templateName, cb, templatePath, templateExtension) {
if(!templates[templateName]) {

// Save template loading state und all callbacks here
templates[templateName] = {
ready: false,
callbacks: [cb]
};

var path = [(templatePath || ich.templatePath), '/',
templateName, '.',
(templateExtension || ich.templateExtension)].join('');


// Request template file async
$.get(path, function(data) {

// Add template to use in callbacks
ich.addTemplate(templateName, data);

// Mark template as loaded
templates[templateName].ready = true;

// Execute all callbacks
for (var i = 0; i < templates[templateName].callbacks.length; i++) {
templates[templateName].callbacks[i]();
};
});
} else if (templates[templateName].ready) {
// If template has been loaded before, execute callback
cb();
} else {
// If template loading was started, add cb to callbacks
templates[templateName].callbacks.push(cb);
}
};


/*
* Make sure to also clean up templates object when calling clearAll()
*/
var clearAll = ich.clearAll;

ich.clearAll = function() {
templates = {};

clearAll();
};

})(window, window.document, window.jQuery)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Finn-Lennart Heemeyer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
132 changes: 132 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!DOCTYPE html>
<html>
<head>

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.css" />

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/ICanHaz.js/0.10.3/ICanHaz.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script type="text/javascript" src="../ICanHaz.load.js"></script>

<script type="text/javascript">

/*
* Test suite
*/
describe("ICanHaz.load", function() {

beforeEach(function() {
ich.clearAll();
});

/*
* Global config
*/
ich.templatePath = "templates";
ich.templateExtension = "mustache";

/*
* Specs
*/

it("should load template", function() {

var spy = {
func: function() {
}
};

spyOn(spy, 'func');

runs(function() {
ich.load('template', spy.func);
});

waitsFor(function() {
return ich.template;
}, "Should create template", 750);

runs(function() {

$('body').append(ich.template({
class: 'test',
text: 'foo'
}));

expect(spy.func).toHaveBeenCalled();
expect($('.test').length).toBe(1);
expect($('.test').text().indexOf('foo')).not.toBe(-1);
});

});


it("should prefer arguments over global config", function() {

runs(function() {
ich.load('template', function() {}, 'tpl', 'mus');
});

waitsFor(function() {
return ich.template;
}, "Should create template", 750);

runs(function() {
$('body').append(ich.template({
id: 'unique',
text: 'foo'
}));

expect($('#unique').length).toBe(1);
expect($('#unique').text().indexOf('foo')).not.toBe(-1);
});
});


it("should request template only once", function() {

spyOn($, 'get');

runs(function() {
for (var i = 0; i < 30; i++) {
ich.load('template', function() {});
}
});

waitsFor(function() {
return $.get.callCount > 0;
}, "Should create template", 750);

runs(function() {
expect($.get.calls.length).toBe(1);
});
});
});


$(function() {

/*
* Execute jasmine html runner
*/

var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 250;

var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};

jasmineEnv.execute();

});
</script>
</head>

<body></body>
</html>
3 changes: 3 additions & 0 deletions test/templates/template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div style="display: none" class="{{class}}">
{{text}}
</div>
3 changes: 3 additions & 0 deletions test/tpl/template.mus
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div style="display: none" id="{{id}}">
{{text}}
</div>

0 comments on commit a3b23f6

Please sign in to comment.