I have a question on how best to structure the following idea:
As a (simplified) model I have a Box
-object which contains some properties, and has a set of Items
.
Such an item
has some general properties (name is the same for all items, value is not), and some specific propererties that are only of use for that particular type of Item
. I also have a function makeLabel() on the items which should return a string representation created from the properties, this is also dependent on the type of Item
.
Here's a quick example of this.
Box-object
function Box(){
this.height;
this.width;
this.depth;
...
this.items = [];
}
ToyItem-object:
function ToyItem(){
this.name; //general
this.description; //general
this.minimumAge;
this.getLabel = function(){
return "This toy (" + this.name + ") is for children age " + this.minimumAge + " and up";
}
}
BookItem-object:
function BookItem(){
this.name; //general
this.description; //general
this.owner;
this.getLabel = function(){
return "This book (" + this.name + ") is owned by " + this.owner;
}
}
I am / will be using AngularJS / $resource on the frontend, and Express/mongoose/mongodb on the backend. So the typical MEAN stack.
The number of items will be variable. I want to be able to 'make up' item
-objects as I go, without having to alter the backend.
Right now I only have an api-endpoint for Box
and the items of the box are just stored as an "array of objects".
When I use AngularJS $resource to query the Box
-endpoint I get the Box-$resource with an array of 'generic objects' back. Is there a way casting them to ToyItem, BookItem,... in order to use my specific defined functions?
It would also be nice to be able to call $save and $delete directly on the item, instead of always updating the box opject.
I have control over the whole stack, so I can make changes to every part. I just want some guidance as to what is the best approach for this type of problem.