-
-
Save anteriovieira/495808df51ca9ec13e7ee1324ddcee0b to your computer and use it in GitHub Desktop.
How to use AngularJS ng.resource.IResource with TypeScript.
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
/// <reference path="angular.d.ts" /> | |
/// <reference path="angular-resource.d.ts" /> | |
interface IEmployee extends ng.resource.IResource<IEmployee> | |
{ | |
id: number; | |
firstName : string; | |
lastName : string; | |
} | |
interface IEmployeeResource extends ng.resource.IResourceClass<IEmployee> | |
{ | |
update(IEmployee) : IEmployee; | |
} | |
angular | |
.module('myapp', ['ngResource']) | |
.factory('EmployeeResource', ['$resource', ($resource : ng.resource.IResourceService) : IEmployeeResource => { | |
// Define your custom actions here as IActionDescriptor | |
var updateAction : ng.resource.IActionDescriptor = { | |
method: 'PUT', | |
isArray: false | |
}; | |
// Return the resource, include your custom actions | |
return <IEmployeeResource> $resource('/api/employee/:id', { id: '@id' }, { | |
update: updateAction | |
}); | |
}]) | |
.controller('TestCtrl', ['EmployeeResource', (Employee : IEmployeeResource) => | |
{ | |
// Get all employees | |
var employees : Array<IEmployee> = Employee.query(); | |
// Get specific employee, and change their last name | |
var employee : IEmployee = Employee.get({ id: 123 }); | |
employee.lastName = 'Smith'; | |
employee.$save(); | |
// Custom action | |
var updatedEmployee : IEmployee = Employee.update({ id: 100, firstName: "John" }); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment