Created
April 26, 2013 16:00
-
-
Save philjones88/5468369 to your computer and use it in GitHub Desktop.
For @rippo on AngularJS services and promises, also shows routing and resolve :)
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
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