Skip to content

Commit

Permalink
Reutilizo controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
canofm committed Sep 2, 2016
1 parent 48eb0df commit 7ea9728
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 49 deletions.
1 change: 1 addition & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ angular.module('booking-app', ['ui.router'])
$urlRouterProvider.otherwise('/');
return $locationProvider.html5Mode(true);
})
.constant('_', window._)
.config(function($stateProvider) {
return $stateProvider
.state('main', {
Expand Down
18 changes: 0 additions & 18 deletions app/modules/propiedades/controllers/crearPropiedadCtrl.js

This file was deleted.

18 changes: 0 additions & 18 deletions app/modules/propiedades/controllers/editarPropiedadCtrl.js

This file was deleted.

18 changes: 18 additions & 0 deletions app/modules/propiedades/controllers/propiedadCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function PropiedadCtrl(PropiedadesHome, propiedad, $state, nombreController) {
var self = this;
self.propiedad = propiedad;
self.tiposDePropiedad = [{ nombre: "Hotel" }, { nombre: "Particular" }]

self.guardar = function () {
PropiedadesHome.upsert(self.propiedad);
};

self.abrirTipoPropiedad = function () {
$state.go("main." + nombreController + "_propiedades." + self.propiedad.tipo.nombre.toLowerCase());
};
};

angular.module("booking-app")
.controller("PropiedadCtrl", PropiedadCtrl);

PropiedadCtrl.$inject = [ "PropiedadesHome", "propiedad", "$state", "nombreController" ];
13 changes: 9 additions & 4 deletions app/modules/propiedades/propiedades.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ angular.module('booking-app')
.state('main.alta_propiedades', {
url: "/propiedades/crear",
templateUrl: "app/modules/propiedades/views/form.html",
controller: "CrearPropiedadCtrl",
controllerAs: "formCtrl"
controller: "PropiedadCtrl",
controllerAs: "formCtrl",
resolve: {
propiedad: function () { return {}; },
nombreController: function () { return "alta"; }
}
})
.state('main.alta_propiedades.hotel', {
views : { "tipo-propiedad": { templateUrl: "app/modules/propiedades/views/hotelForm.html" } }
Expand All @@ -27,12 +31,13 @@ angular.module('booking-app')
.state('main.editar_propiedades', {
url: "/propiedades/editar/:id",
templateUrl: "app/modules/propiedades/views/form.html",
controller: "EditarPropiedadCtrl",
controller: "PropiedadCtrl",
controllerAs: "formCtrl",
resolve: {
propiedad: function (PropiedadesHome, $stateParams) {
return PropiedadesHome.get(parseInt($stateParams.id));
}
},
nombreController: function () { return "editar"; }
}
})
.state('main.editar_propiedades.hotel', {
Expand Down
15 changes: 8 additions & 7 deletions app/services/propiedadesHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ function PropiedadesHome() {
self.get = function (id) {
return _.find(propiedades, { id: id });
};

self.create = function (propiedad) {
propiedades.push(_.assign(propiedad, { id : propiedadIdActual++ }));
};

self.update = function (propiedad) {
_.assign(_.find(propiedades, function (obj) { _.isMatch(obj, {id: propiedad.id})}), propiedad)
}
self.upsert = function (propiedad) {
var propiedadExiste = _.find(propiedades, function (obj) { return _.isMatch(obj, { id: propiedad.id }); });
if (propiedadExiste) {
_.assign(propiedadExiste, propiedad);
} else {
propiedades.push(_.assign(propiedad, { id: propiedadIdActual++ }));
}
};

self.delete = function (id) {
_.remove(propiedades, function(obj) { return _.isMatch(obj, { id: id }); });
Expand Down
3 changes: 1 addition & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
<script src="app/modules/dashboard/routes/dashboard.js"></script>
<script src="app/modules/dashboard/controllers/dashboard.js"></script>
<script src="app/modules/propiedades/controllers/listadoPropiedadesCtrl.js"></script>
<script src="app/modules/propiedades/controllers/crearPropiedadCtrl.js"></script>
<script src="app/modules/propiedades/controllers/editarPropiedadCtrl.js"></script>
<script src="app/modules/propiedades/controllers/propiedadCtrl.js"></script>
<script src="app/modules/propiedades/propiedades.js"></script>
<script src="app/services/propiedadesHome.js"></script>
<script src="app/directives/cajita/cajita.js"></script>
Expand Down

0 comments on commit 7ea9728

Please sign in to comment.