Skip to content

Instantly share code, notes, and snippets.

@bradkovach
Forked from scottmcarthur/app.ts
Last active September 5, 2017 18:39
Show Gist options
  • Save bradkovach/eb6428fda3446ad0d893da986733bccc to your computer and use it in GitHub Desktop.
Save bradkovach/eb6428fda3446ad0d893da986733bccc to your computer and use it in GitHub Desktop.
How to use AngularJS ng.resource.IResource with TypeScript.
/// <reference path="angular.d.ts" />
/// <reference path="angular-resource.d.ts" />
/*
The intent of this fork is to show you how to separate your IResource from your Interfaces.
We had a need in our project to keep the resource separate from the business entity, so this
updated example reflects those changes.
Furthermore, I've added TSDoc (comments starting with /**) comments, which your IDE should be
able to pick up and present while you work.
*/
/*
Consumers of this module can use the IEmployee or IEmployeeResource types, depending on their needs
*/
/**
* Intent-free interface representing an IEmployee. If you are working with the EmployeeResource,
* you probably need IEmployeeResource
*/
interface IEmployee {
/**
* The ID of the Employee
*/
id: number;
/**
* The First Name of the employee, optional
*/
firstName ?: string;
/**
* The Last Name of the employee, optional
*/
lastName ?: string;
}
/**
* This union type provides all of the properties of IEmployee and all of the mutators of IResource.
* Includes methods like $save, $update
*/
type IEmployeeResource = IEmployee & ng.resource.IResource<IEmployee>;
/*
You can selectively overload signatures on the IEmployeeResource here. This is optional.
*/
/**
* IEmployeeResourceClass provides methods to create or retrieve IEmployeeResource objects.
* Notice that the update method is overloaded.
*/
interface IEmployeeResourceClass extends ng.resource.IResourceClass<IEmployeeResource>
{
update(employee: IEmployee) : IEmployeeResource;
}
angular
.module('myapp', ['ngResource'])
/*
This is the factory method that generates and returns the IEmployeeResourceClass, which is the type used
when you inject the EmployeeResource
*/
.factory('EmployeeResource', ['$resource', ($resource : ng.resource.IResourceService) : IEmployeeResourceClass => {
// Define your custom actions here as IActionDescriptor
var updateAction : ng.resource.IActionDescriptor = {
method: 'PUT',
isArray: false
};
// Return the resource, include your custom actions
return <IEmployeeResourceClass> $resource('/api/employee/:id', { id: '@id' }, {
// This is the attachment of the overloaded signature
update: updateAction
});
}])
/*
Note that the Employee parameter's type is `IEmployeeResourceClass`
*/
.controller('TestCtrl', ['EmployeeResource', (Employee : IEmployeeResourceClass) =>
{
// Get all employees
var employees : Array<IEmployeeResource> = Employee.query();
// Do something with the result
employees.forEach( employee => console.log(`Hello, ${employee.firstName}`) )
// Get specific employee
var employee : IEmployeeResource = Employee.get({ id: 123 });
// Change their last name
employee.lastName = 'Smith';
// Most of these methods are chainable
employee
// Save the change... This returns an angular.IPromise
.$save()
// Do something with the result of the save (optional)
.then(success => console.log(`Employee ${success.id} saved`) );
// Custom action
var updatedEmployee : IEmployeeResource = Employee.update({ id: 100, firstName: "John" });
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment