Here's the current code snippet I'm working with:
// Factory
angular.factory('Student', function() {
var defaults = {
StudentId: null
};
var Student = function(params) {
angular.extend(this, angular.copy(defaults), params);
};
// Prototype function for Student
Student.prototype.hasStudentId = function() {
return this.StudentId != null;
};
});
// Service
angular.service('StudentService', ['Student', '$resource', function(Student, $resource) {
var service = {};
service.student = $resource('student/:studentId'. {} { load: {method: 'GET'} });
service.loadStudent = function(studentId) {
var currentStudent = service.student.load(studentId); // HTTP call
currentStudent.$promise.then(function() {
// When extending currentStudent, it doesn't have the hasStudentId() prototype function
angular.extend(currentStudent, new Student(currentStudent));
// However, using 'new' keyword gives studentObj the proper prototype hasStudentId() function
var studentObj = new Student(currentStudent);
});
};
return service;
}]);
After using Angular.extend on currentStudent
, the object does not contain the hasStudentId()
prototype function. If I instantiate a new instance with new Student(currentStudent)
, it works correctly.
Is Angular.extend
disregarding the prototype
functions?
At the moment, currentStudent
only inherits the prototype functions of $resource
.