Skip to content

Instantly share code, notes, and snippets.

@philjones88
Created April 26, 2013 16:00
Show Gist options
  • Save philjones88/5468369 to your computer and use it in GitHub Desktop.
Save philjones88/5468369 to your computer and use it in GitHub Desktop.
For @rippo on AngularJS services and promises, also shows routing and resolve :)
var app = angular.module('sample', ['ngSanitize'], function ($routeProvider) {
var $base = "/admin/partial?path=Admin/PartialViews/";
$routeProvider
.when("/offer", {
templateUrl: $base + 'Offer/list.cshtml', controller: OfferListController, resolve: {
model: function (offerService) {
return offerService.list();
}
}
});
});
app.factory("offerService", function ($http, $location, $log) {
var url = "/admin/api/offer";
return {
list: function () {
$log.info("offerService.list");
return $http.get(url);
},
deleteOffer: function (offerId) {
$log.info("offerService.deleteOffer : {" + offerId + "}");
return $http.delete(url + "/" + offerId);
}
};
});
function OfferListController($scope, model, offerService) {
$scope.model = model.data;
$scope.service = offerService;
$scope.delete = function (item) {
if (confirm("Are you sure you want to delete this offer?")) {
$scope.service.deleteOffer(item.idInt).success(function () {
$scope.model = _($scope.model).reject(function (el) { return el.id === item.id; }); // just underscore.js to remove it from array
$scope.toastSuccess("Deleted offer successfully"); // notify ui, https://github.com/CodeSeven/toastr
});
// could also attach .error() promise but don't bother for this example
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment