Home / Companies / Sauce Labs / Blog / Post Details
Content Deep Dive

Adding Custom Methods to Data Models with Angular $resource

Blog post from Sauce Labs

Post Details
Company
Date Published
Author
Bill McGee
Word Count
407
Language
English
Hacker News Points
-
Summary

Angular $resource allows developers to create custom methods for data models by extending the prototype of the resource model. This approach is more efficient than other methods, as it only requires adding behavior to individual job instances, rather than duplicating code across multiple instances. The example uses `angular.extend()` to add a `getResult()` method to each job instance, allowing developers to easily iterate over the jobs and display their outcome in a template. The revised controller also takes advantage of the promise returned by `$resource`, making it simpler for developers to handle data loading and rendering. This approach makes $resource a more attractive option for teams with simple applications or those who want to implement custom behavior without excessive code duplication.