I am new to using angular and had a question regarding adding behavior to objects in angular.
I have a case in which I have getting an object / or array of objects say person using $http
var person =
{
id : 123,
firstname : "james",
lastname : "bond",
username : "james00",
profileGuid : "DSKFSJKFBSFBJBSFKJBAKBDS",
projects : [
{
id : 1,
name : "gold finger"
}
]
}
I want to add behavior like say
var spyBehavior =
{
greet : function(){
return this.lastname + " " + this.firstName + " " + this.lastName;
},
hasExperience : function(){
this.projects && this.projects.length && this.projects.length > 0
}
}
Currently I do this with angular.extend(person, spyBehavior)
- What is the pros/cons of adding behavior using such
spyBehaviorobject? - Should this be defined as an angular service ? - in which case, I loose the reference of
this - Should this be outside angular world and exist as a simple javascript object which wraps around $http output ? how do I do it?