Last active
August 29, 2015 14:15
-
-
Save vic/6e14f1419fc82cab6bb4 to your computer and use it in GitHub Desktop.
Patos as a service
This file contains 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
Patos demo | |
How to serve: | |
`python -m SimpleHTTPServer` | |
open http://localhost:8000 |
This file contains 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="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> | |
<script src="index.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body ng-app="patito"> | |
<br /> | |
<div ng-controller="MainCtrl"> | |
{{ hola }} {{ nombre }} | |
<label> | |
Vuela vuela? | |
<input type="checkbox" ng-model="volador" | |
ng-true-value="true" | |
ng-false-value="false" /> | |
</label> | |
<label>Nombre</label> | |
<input type="text" ng-model="nombre" /> | |
<div ng-if="volador"> | |
Esta madre vuela | |
</div> | |
<div ng-if="!volador"> | |
Esta madre se arrastra | |
</div> | |
<div ng-controller="PatoEditorCtrl"> | |
<h3>Crea un Pato</h3> | |
<input type="text" ng-model="pato.nombre" /> | |
<input type="number" ng-model="pato.edad" min=1 max=20 /> | |
<input type="checkbox" ng-model="pato.vuela" | |
ng-true-value="true" | |
ng-false-value="false" /> | |
<input type="button" value="Crear Pato" ng-click="save()" /> | |
</div> | |
<div> | |
<h3>Tus patos</h3> | |
<ul> | |
<li ng-repeat="pato in patoList()"> | |
<pato-show pato="pato" /> | |
</li> | |
</ul> | |
</div> | |
</div> | |
Hola | |
<script type="text/template" id="pato-template"> | |
<i>Hola yo soy un pato {{ pato.nombre }} </i> | |
</script> | |
</body> | |
</html> |
This file contains 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("patito", []). | |
service('PatosService', PatosService). | |
directive('patoShow', PatoElement). | |
controller('PatoEditorCtrl', PatoEditorCtrl). | |
controller('PatoCtrl', PatoCtrl). | |
controller('MainCtrl', MainCtrl) | |
; | |
MainCtrl.$inject = ['$scope', 'PatosService']; | |
function MainCtrl ($scope, bolsaDePatos) { | |
$scope.hola = "Hello world"; | |
$scope.nombre = "Luis" | |
$scope.volador = true; | |
$scope.patoList = bolsaDePatos.findAll; | |
} | |
function PatoEditorCtrl ($scope, PatosService) { | |
var patoGenerico = { | |
nombre: "Hugo", | |
edad: 10, | |
vuela: true | |
}; | |
$scope.pato = Object.create(patoGenerico); | |
$scope.save = function () { | |
console.log("Creando pato ", $scope.pato); | |
PatosService.create($scope.pato); | |
$scope.pato = Object.create(patoGenerico); | |
} | |
} | |
function PatosService () { | |
var patos = []; | |
this.findAll = function () { | |
return patos; | |
} | |
this.create = function (pato) { | |
patos.push(pato); | |
} | |
} | |
function PatoCtrl ($scope) { | |
} | |
function PatoElement () { | |
return { | |
restrict: 'E', | |
scope: { | |
pato: '=' | |
}, | |
controller: 'PatoCtrl', | |
template: document.getElementById('pato-template').innerHTML | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment