Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Last active December 18, 2015 04:49
Show Gist options
  • Select an option

  • Save alexbeletsky/ba1e1fc270873987f6aa to your computer and use it in GitHub Desktop.

Select an option

Save alexbeletsky/ba1e1fc270873987f6aa to your computer and use it in GitHub Desktop.
// IIF
app.namespace('app.module').calculator = (function () {
// private implementation
var x = 0;
function calculate() {
return x;
}
// public interface
return {
add: function (input) {
return calculate(input);
}
};
})();
describe('module', function () {
it ('should see x', function () {
expect(app.module.calculator.add('1,2,3')).toEqual(0);
});
it('x is in closure (means private)', function () {
expect(app.module.calculator.x).not.toBeDefined();
});
it('calculate is private', function () {
expect(app.module.calculator.calculate).not.toBeDefined();
});
});
var app = app || {};
app.namespace = (function (global) {
return function (namespace) {
var splitted = namespace.split(".");
return _.reduce(splitted, function(prev, next) {
if (!prev[next]) prev[next] = { };
return prev[next];
}, global);
};
})(this);
describe('namespace', function () {
describe('usege', function () {
beforeEach(function () {
app.namespace('app.widgets').MyWidget = function () {
};
});
it('should be inside namespace', function () {
var o = app.widgets.MyWidget;
expect(o).toBeDefined();
});
describe('SuperWidget.js', function () {
beforeEach(function () {
app.namespace('app.widgets.SuperWidgets').MyWidget = function () {
};
});
it('nested namespace objects', function () {
expect(app.widgets.SuperWidgets.MyWidget).toBeDefined();
});
});
});
});
function Task() {
// non inheritable
this.onClick = function () {
};
this.onSome = function () {
};
this.initialize();
}
_.extend(Task.prototype, (function () {
var _somePrivateFunction = function () {
};
// inheritable
return {
foo: function () {
return _somePrivateFunction();
},
boo: function () {
}
};
})());
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-1.1.0.rc1/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="components/jasmine/lib/jasmine-core/jasmine.css">
<script type="text/javascript" src="components/jquery/jquery.js"></script>
<script type="text/javascript" src="components/jasmine/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="components/jasmine/lib/jasmine-core/jasmine-html.js"></script>
<script type="text/javascript" src="components/underscore/underscore.js"></script>
<script type="text/javascript" src="spec/namespace.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var trivialReporter = new jasmine.TrivialReporter();
jasmineEnv.addReporter(trivialReporter);
jasmineEnv.specFilter = function(spec) {
return trivialReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
</script>
</head>
<script id="product-simple" type="text/x-template">
<div class="product"><span><%= name %> <%= price %></span></div>
</script>
<script id="product-template" type="text/x-template">
<% _.each(products, function(product) { %><div class="product"><span><%= product.name %> <%= product.price %></span></div><% }); %>
</script>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment