I am facing serious headaches that are trying to create factories for one of my angular modules. I need to take some JSON data and be able to work with it, preserving the original format so that I can return it back to the 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 instead of tracking the structure in my view.
For this, 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);
})
This part works great. The problem is that I also want to add angular access methods to allow me to get / set nested values ββsuch as gender. sort of:
get gender(){ return this.phys.gender; }
, factory. , , -
function DataObject(data, valueBuilder) {}
return DataObject;
return { get gender(), set gender(val)};
, factory . , - . ?