Last active
August 15, 2019 12:39
-
-
Save scottmcarthur/9005953 to your computer and use it in GitHub Desktop.
How to use AngularJS ng.resource.IResource with TypeScript.
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
/// <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
i made a customize version of ngResource, which i transform result after they become ready, the original function transformResponse change and destroy type of data, so i intruduce a new function called transformResult, but how should i introduce this callback/config to the TS ?