It is possible to define an Angular $resource
that has always the same default values on creation with new()
?
For example if I have the following resource definition:
var Drawer = $resource('/drawer/:drawerId', { drawerId: '@id'});
And the Drawer object needs to have a "socks" property that I want to be always initialized as an empty array []
, and maybe some others like 'timesOpened' to 0, or things like that.
The only way to do this would be like:
var newDrawer = new Drawer({ socks: [], timesOpened: 0});
I was thinking about defining in the same service I have for my resource (let's call it drawerService
) a default/initialization object like this:
defaultDrawer = { socks: [], timesOpened: 0};
And then when creating the resource:
//New drawer with defaults
var newDrawer = new drawerService.Drawer(drawerService.defaultDrawer);
//New drawer with defaults and some other initialization
var anotherDrawer = new drawerService.Drawer(angular.extend(drawerService.defaultDrawer, {material: 'plastic'});
Is this the only way of doing it?