var angular;
var app = angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/dashboard', { templateUrl: 'template/books.html', controller: 'CustomerListControler' })
.when('/NewCustomer', { templateUrl: 'template/formNew.html', controller: 'CustomerAddControler' })
.when('/UpdateCustomer/:id', { templateUrl: 'template/form.html', controller: 'CustomerEditControler' })
.when('/entrar', { templateUrl: 'template/entrar.html', controller: 'EntrarControler' })
.otherwise({ redirectTo: '/dashboard' });
}]);
app.controller('CustomerListControler', [
'$scope', '$http',
function($scope, $http) {
$http.get('api/Customers').success(function(data) {
$scope.customers = data;
});
}
]),
app.controller('EntrarControler', [
'$scope', '$http', '$location', '$routeParams',
function($scope, $http, $location, $routeParams) {
$scope.master = {};
$scope.activePath = null;
$scope.New_Customer = function(customer, AddNewForm) {
console.log(customer);
$http.post('api/entrar', customer).success(function() {
$scope.reset();
$scope.activePath = $location.path('/dashboard');
});
$scope.reset = function() {
$scope.customer = angular.copy($scope.master);
};
$scope.reset();
}
}
]),
app.controller('CustomerAddControler', [
'$scope', '$http', '$location', '$routeParams',
function($scope, $http, $location, $routeParams) {
$scope.master = {};
$scope.activePath = null;
$scope.New_Customer = function(customer, AddNewForm) {
console.log(customer);
var data_name = $routeParams.data_name;
if (data_name == true) {
$http.get('api/Book/' + data_name).success(function() {
$scope.reset();
$scope.activePath = $location.path('/NewCustomer');
});
$scope.mymensaje = "datos encontrado";
}
else {
$http.post('api/New_Customer', customer).success(function() {
$scope.reset();
$scope.activePath = $location.path('/dashboard');
});
$scope.reset = function() {
$scope.customer = angular.copy($scope.master);
};
$scope.reset();
alert('datos agregados');
}
}
}
]),
app.controller('CustomerEditControler', [
'$scope', '$http', '$location', '$routeParams',
function($scope, $http, $location, $routeParams) {
var id = $routeParams.id;
$scope.activePath = null;
$http.get('api/Customers/' + id).success(function(data) {
$scope.CustomerDetail = data;
});
$scope.Update_Customer = function(customer) {
var updateCustomer = confirm('desea modificar los datos de: ' + id);
if (updateCustomer) {
$http.put('api/Customers/' + id, customer).success(function(data) {
$scope.CustomerDetail = data;
$scope.activePath = $location.path('/dashboard');
});
}
$scope.mensaje = "datos actualizados";
}
$scope.Delete_Customer = function(customer) {
console.log(customer);
var deleteCustomer = confirm('estais seguro querer borrar registro?' + id);
if (deleteCustomer) {
$http.delete('api/Customers/' + customer.id);
$scope.activePath = $location.path('/dashboard');
}
};
}
]);