2017-04-30 20:02:47 +03:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module("app").controller("EditCtrl", Ctrl);
|
2017-06-10 23:18:35 +03:00
|
|
|
Ctrl.$inject = ['$http', '$routeParams', '$location', 'contactsService']
|
2017-04-30 20:02:47 +03:00
|
|
|
|
2017-06-10 23:18:35 +03:00
|
|
|
function Ctrl($http, $routeParams, $location, contactsService) {
|
2017-04-30 20:02:47 +03:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
vm.contact = {};
|
|
|
|
vm.submitForm = submitForm;
|
|
|
|
|
2017-06-10 23:18:35 +03:00
|
|
|
var contactId = $routeParams.id;
|
|
|
|
|
|
|
|
getContact(contactId)
|
2017-04-30 20:02:47 +03:00
|
|
|
function getContact(id) {
|
|
|
|
if(!id) return
|
|
|
|
|
2017-06-10 23:18:35 +03:00
|
|
|
contactsService.getContact(id).then(function(contact) {
|
|
|
|
vm.contact = contact;
|
|
|
|
})
|
2017-04-30 20:02:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function submitForm() {
|
2017-06-10 22:25:11 +03:00
|
|
|
console.log("submitting contact", vm.contact);
|
2017-06-10 23:18:35 +03:00
|
|
|
if(contactId) {
|
|
|
|
contactsService.editContact(contactId, vm.contact).then(function(resp){
|
2017-04-30 20:02:47 +03:00
|
|
|
$location.path("/search");
|
|
|
|
});
|
|
|
|
} else {
|
2017-06-10 23:18:35 +03:00
|
|
|
contactsService.addContact(vm.contact).then(function(resp){
|
2017-04-30 20:02:47 +03:00
|
|
|
$location.path("/search");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})();
|