Last active
December 13, 2015 23:08
-
-
Save danypype/4989514 to your computer and use it in GitHub Desktop.
A CodePen by Daniel López. Using model aggregation and inheritance in AngularJS
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src='http://code.angularjs.org/1.1.2/angular.min.js'> | |
</script> | |
</head> | |
<body ng-app='App'> | |
<div> | |
<h2>Agregation</h2> | |
<h3>ClassB</h3> | |
<classb></classb> | |
<h3>ClassA wich has ClassB</h3> | |
<classa></classa> | |
</div> | |
<br/> | |
<div> | |
<h2>Inheritance</h2> | |
<h3>Person</h3> | |
<person></person> | |
<h3>Developer whose inherits from Person</h3> | |
<developer></developer> | |
</div> | |
</body> | |
</html> |
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
angular.module('App', []) | |
//Agregation | |
.factory('ClassB', function(){ | |
var ClassB = function(config){ | |
this.name = config.name; | |
} | |
return ClassB; | |
}) | |
.factory('ClassA', ['ClassB', function(ClassB){ | |
var ClassA = function(config){ | |
this.name = config.name; | |
var configB = {name:'Child of a ClassA'}; | |
this.objB = new ClassB(configB); | |
} | |
return ClassA; | |
}]) | |
.controller('ClassACtrl', ['$scope', 'ClassA', function($scope, ClassA){ | |
if(!$scope.model){ | |
config = {name: 'ClassA'}; | |
$scope.objA = new ClassA(config); | |
}else{ | |
$scope.objA = $scope.model; | |
} | |
}]) | |
.controller('ClassBCtrl', ['$scope', 'ClassB', function($scope, ClassB){ | |
if(!$scope.model){ | |
config = {name: 'ClassB'}; | |
$scope.objB = new ClassB(config); | |
}else{ | |
$scope.objB = $scope.model; | |
} | |
}]) | |
.directive('classa', function(){ | |
return { | |
restrict: 'E', | |
scope: {model: '='}, | |
controller: 'ClassACtrl', | |
template: | |
"<div>"+ | |
"{{objA.name}}/"+ | |
"{{objA.objB.name}}"+ | |
"<input type='text' ng-model='objA.objB.name'/>"+ | |
"<classb model='objA.objB'></classb>"+ | |
"</div>", | |
replace: true | |
} | |
}) | |
.directive('classb', function(){ | |
return { | |
restrict: 'E', | |
scope: {model: '='}, | |
controller: 'ClassBCtrl', | |
template: | |
"<div>"+ | |
"{{objB.name}}"+ | |
"<input type='text' ng-model='objB.name'/>"+ | |
"</div>", | |
replace: true | |
} | |
}) | |
//Inheritance | |
.factory('Person', function(){ | |
var Person = function(config){ | |
this.ate = []; | |
if(config){ | |
this.firstName = config.firstName; | |
this.lastName = config.lastName; | |
} | |
} | |
Person.prototype.eat = function(food){ | |
this.ate.push(food); | |
} | |
return Person; | |
}) | |
.factory('Developer', ['Person', function(Person){ | |
Developer.prototype = new Person(); | |
Developer.prototype.constructor = Developer; | |
function Developer(config){ | |
Person.prototype.constructor.call(this, config); | |
this.techs = []; | |
if(config){ | |
this.lastName = config.lastName + ' San'; | |
} | |
} | |
Developer.prototype.learn = function(tech){ | |
this.techs.push(tech); | |
} | |
return Developer; | |
}]) | |
.controller('PersonCtrl', ['$scope', 'Person', function($scope, Person){ | |
if($scope.model){ | |
$scope.person = $scope.model; | |
}else{ | |
var config = {firstName:'Daniel', lastName:'López'}; | |
$scope.person = new Person(config); | |
} | |
$scope.newFood = ''; | |
$scope.eat = function(food){ | |
$scope.person.eat(food); | |
$scope.newFood = ''; | |
} | |
}]). | |
controller('DeveloperCtrl', ['$scope', 'Developer', function($scope, Developer){ | |
if($scope.model){ | |
$scope.developer = $scope.model; | |
}else{ | |
var config = {firstName:'Daniel', lastName:'López'}; | |
$scope.developer = new Developer(config); | |
} | |
$scope.newTech = ''; | |
$scope.learn = function(tech){ | |
$scope.developer.learn(tech); | |
$scope.newTech = ''; | |
} | |
}]). | |
directive('person', function(){ | |
return { | |
restrict: 'E', | |
scope: {model: '='}, | |
controller: 'PersonCtrl', | |
template: | |
"<div>"+ | |
"My name is {{person.firstName}} {{person.lastName}}"+ | |
"<p>I ate:</p>"+ | |
"<ul>"+ | |
"<li ng-repeat='food in person.ate'>{{food}}</li>"+ | |
"</ul>"+ | |
"<label>Eat:"+ | |
"<input type='text' ng-model='newFood'/>"+ | |
"<input type='button' value='go' ng-click='eat(newFood)'/>"+ | |
"</label>"+ | |
"</div>", | |
replace: true | |
} | |
}) | |
.directive('developer', function(){ | |
return { | |
restrict: 'E', | |
scope: {model: '='}, | |
controller: 'DeveloperCtrl', | |
template: | |
"<div>"+ | |
"<person model='developer'></person>"+ | |
"<p>I know:</p>"+ | |
"<ul>"+ | |
"<li ng-repeat='tech in developer.techs'>{{tech}}</li>"+ | |
"</ul>"+ | |
"<label>Learn:"+ | |
"<input type='text' ng-model='newTech'/>"+ | |
"<input type='button' value='go' ng-click='learn(newTech)'/>"+ | |
"</label>"+ | |
"</div>", | |
replace: true | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CodePen link: http://codepen.io/danypype/pen/xpfuv