I am running into some major headaches trying to create factories for one of my angular modules. I need to take some JSON data and be able to work with it while still keeping the original format so I can turn it back into a string. so if I have JSON data like:
[ [ '11/09/2012', {gender:'M'}, 'John', 'Smith'], ['07/22/1986', {gender:'M'}, 'Bill', 'Miller], ...]
I want to be able to call data.birthday or data.firstName rather than having to keep track of the structure in my view.
To do that I have a factory that looks like this:
.factory('DataObject', function () {
DataObject.prototype.objToArray = function()
{
var arrayVal = [];
arrayVal[0] = this.bday;
arrayVal[1] = this.phys;
arrayVal[2] = this.fname;
arrayVal[3] = this.lname;
return arrayVal;
};
function DataObject(data, valueBuilder) {
if(data)
{
this.bday = data[0];
this.phys = data[1];
this.fname = data[2];
this.lname = data[3];
}
}
return (DataObject);
})
That part works fine. The problem is, I also want to add angular accessor methods to allow me to get/set nested values like gender. something like:
get gender(){ return this.phys.gender; }
I can not figure out how to use that syntax with my factory. The only way It will allow me to use accessor methods is to change
function DataObject(data, valueBuilder) {}
return DataObject;
to
return { get gender(), set gender(val)};
which means I have no way to actually instantiate my factory with the data. I'm sure there must be some way to do this. What am I missing?