Created
January 2, 2015 07:36
-
-
Save wcadap/d7bfe8510f59040983e5 to your computer and use it in GitHub Desktop.
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
contactApp.factory('contactDataService', ['$http', '$q', | |
function ($http, $q) { | |
var _contacts = []; | |
var _getContacts = function () { | |
var deferred = $q.defer(); | |
var controllerQuery = "contact/GetContacts"; | |
$http.get(controllerQuery) | |
.then(function (result) { | |
// Successful | |
angular.copy(result.data, _contacts); | |
deferred.resolve(); | |
}, | |
function (error) { | |
// Error | |
deferred.reject(); | |
}); | |
return deferred.promise; | |
} | |
var _addContact = function (_contact) { | |
var deferred = $q.defer(); | |
var controllerQuery = "contact/AddContact"; | |
$http.post(controllerQuery, _contact) | |
.then(function (result) { | |
//Success | |
deferred.resolve(); | |
}, | |
function (error) { | |
// Error | |
deferred.reject(); | |
}); | |
return deferred.promise; | |
}; | |
var _updateContact = function (_contact) { | |
var deferred = $q.defer(); | |
var controllerQuery = "contact/UpdateContact"; | |
$http.post(controllerQuery, _contact) | |
.then(function (result) { | |
deferred.resolve(); | |
}, | |
function (error) { | |
// Error | |
deferred.reject(); | |
}); | |
return deferred.promise; | |
}; | |
var _deleteContact = function (id) { | |
var deferred = $q.defer(); | |
var controllerQuery = "contact/DeleteContact/"+ id; | |
$http.post(controllerQuery) | |
.then(function (result) { | |
deferred.resolve(); | |
}, | |
function (error) { | |
// Error | |
deferred.reject(); | |
}); | |
return deferred.promise; | |
}; | |
function _findContactById(id) { | |
var found = null; | |
$.each(_contacts, function (i, contact) { | |
if (contact.id == id) { | |
found = contact; | |
return false; | |
} | |
}); | |
return found; | |
}; | |
//Expose methods and fields through revealing pattern | |
return { | |
contacts: _contacts, | |
getContacts: _getContacts, | |
addContact: _addContact, | |
updateContact: _updateContact, | |
deleteContact : _deleteContact, | |
findContactById: _findContactById | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment