As a newcomer to AngularJS, I have created a factory class to generate instances of my "Case" class. However, I'm facing an issue where creating new instances of "Case" seems to affect my existing instances as well. Despite being simple, I can't seem to solve this problem.
I thought I was being clever by designing a basic (generic) class.
Here's my factory class:
.factory('Case', function($q, $http) {
var optionsProto = {
id : null,
reference : "",
fields : []
}
var self = this;
return function Case(options) {
angular.extend(self, optionsProto, options);
return self;
// Updates via. webservice, if possible
this.update = function get(options) {
// not implemented yet
return self;
};
// Saves via. webservice, if possible
this.save = function save() {
// not implemented yet
return self;
};
}
})
And here's my controller:
.controller('CasesCtrl', function($scope, Case) {
$scope.cases = [
new Case({"id": 1}),
new Case({"id": 2}),
new Case({"id": 3}),
];
console.log($scope.cases);
})
The console output appears as follows:
Object {id: 3}
Object {id: 3}
Object {id: 3}