Having trouble with implementing getters and setters for model objects in Angular. Facing an error:
TypeError: Cannot read property 'firstName' of undefined
at User.firstName (http://run.plnkr.co/AvdF2lngjKB76oUe/app.js:35:32)
The code snippet:
angular.module('getterSetterExample', [])
.controller('ExampleController', ['$scope', function($scope) {
var intObj = { firstName: 'Brian' };
$scope.user = new User(intObj);
}]);
function ModelBase(wo) {
this.wrappedObject = wo;
this.onPropertyChanged = function(self, propertyName, oldValue, newValue) {
//alert(self + ", " + propertyName + ", " + oldValue + ", " + newValue);
}
}
var isDefined = function(value) {
return typeof value !== 'undefined';
};
User.prototype = new ModelBase();
User.prototype.constructor = User;
function User(wo) {
ModelBase.call(this, wo);
this.firstName = function(value) {
if(isDefined(value))
{
var oldValue = this.wrappedObject.firstName;
this.wrappedObject.firstName = value;
}
else
{
return this.wrappedObject.firstName; //(Line 32)
}
}
}
Seems like the getter is being called before wrappedObject is set on the base object. Any insights into what might be missing here? Included onPropertyChanged, but it's commented out to better illustrate the goal.