Created
February 8, 2012 23:31
-
-
Save namsral/1775417 to your computer and use it in GitHub Desktop.
Testing AngularJS
This file contains hidden or 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
// service.js | |
angular.service('Address', function($resource, $cookies, $xhr, $log) { | |
// Add Header to comply with Django's CSRF implementation | |
token = $cookies['csrftoken']; | |
$xhr.defaults.headers['delete']['X-CSRFToken'] = token; | |
return $resource('/api/user/address/:addressId', {addressId:'@id'}, {}); | |
}); | |
// controller.js | |
function AddressCtrl(Address) { | |
var scope = this | |
scope.address = Address.query(); | |
scope.newAddress = function(){ | |
$log.log('new address.'); | |
} | |
scope.deleteAddress = function(addressToRemove){ | |
$log.log('Remove: '+addressToRemove.id); | |
addressToRemove.$delete({addressId:addressToRemove.id}, | |
function() { | |
$log.log('success') | |
var index = scope.address.indexOf(addressToRemove); | |
scope.address.splice(index, 1); | |
}, | |
function(){ | |
$log.log('error') | |
} | |
)}; | |
} | |
AddressCtrl.$inject = ['Address']; | |
// address_template.html | |
<ul> | |
<li ng:repeat="i in address.$orderBy('id')">{{ i.email }} <button ng:click="deleteAddress(i)">delete</button></li> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment