Bring Class into Javascript with extendable and bindable feature.
Download the Production version or the Development version.
Or download it with bower: open terminal and run
bower install bower-angular-closure
Include js files into your web page:
<script type="text/javascript" src="[...]/closure[.min].js"></script>
Add dependency to your app module:
angular.module('your-app-name', [
'angular-closure'
]);
The closure
module is now installed. It exposes the ClosureProvider
provider and Closure
factory into your app.
angular.module('app', [
'angular-closure'
]).run(function(Closure, $http){
///////////////////////////////////////////////////
// Create object A
///////////////////////////////////////////////////
A = Closure.extend({
value: null,
init: function() {
this.value = 0;
},
get: function() {
return this.value;
},
set: function(value) {
this.value = value;
}
});
// A.init will be called automatically
// A.value is equal 0
// A.get() will return 0
// if A.set(100) then A.value and A.get() will be equal 100
///////////////////////////////////////////////////
// Extend A object
///////////////////////////////////////////////////
A.extend({
init: function() {
this.value = 200;
},
get: function() {
return 'value=' + this._super();
},
test: function(){
return 'hello moto';
}
});
// A.value is equal 200
// A.get() is equal `value=200`. `this._super` will refer to parent function which return `this.value`
// A.test() is equal `hello moto`
});
Note:
this._super()
does not work inasync
because it will be cleared up at the end of the function. You may need to usevar _super = this._super
before callasync
.- You can extend many levels as you want.
- Be careful with
this
object. Take advantage of the best practice below:
angular.module('app', [
'angular-closure'
]).run(function(Closure){
///////////////////////////////////////////////////
// bind `this` object inside callback
///////////////////////////////////////////////////
var B = Closure.extend({
values: [],
init: function(){
angular.forEach([1,2,3,4,5], Closure.bind(function(value){
this.values.push(value);
}, this));
}
});
// B.values is equal [1,2,3,4,5]
});
See Getting started
See CHANGELOG.md
See CONTRIBUTING.md
MIT - Copyright (c) 2014 Angularfiy.org & HenryTao.