Skip to content

Commit

Permalink
Merge ba45beb into 3291c73
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeoverall committed Sep 7, 2014
2 parents 3291c73 + ba45beb commit 31f5dc8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/FirebaseArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,41 @@
return $firebaseUtils.reject('Invalid record; could not find key: '+indexOrItem);
}
},

/**
* With many instances I create userobjects that are tied to user accounts using firebaseSimpleLogin. I often find that I want to assign those user objects to another object.
* Example:
* Lets say I have a list of userObjects called Teachers
* I also have a seperate list of userObjects called Students
* now I want to assign my teachers a list of students that I can add and remove easily using an ng-repeat for studentObj in students
* so I set $scope.teacher.students to be a firebase Array.
*
* I should then be able to use something like $scope.teacher.$Add(studentObj) and $scope.teacher.$remove(studentObj)
* however I was running into a problem that would allow me to add students but would not be able to remove the students
* because I did not have a reference to their unique $id after they are added to the teacher.students array.
*
* my solution to this problem was to check my teacher.students array for the correct email as it is the one constant between userObjects
* if that email was found I created a reference to the array object from which I could get the unique $id and have it passed to the normal firebase $remove method.
* this method is of course specfic to removing userObjects alone.
*/

$removeUserObject: function (arr, object) {
var found = false;
var ref = {};
this._assertNotDestroyed('$remove');
for (var i = 0; i < arr.length; i++) {
if (arr[i].email === object.email) {
ref = arr[i];
found = true;
break;
}
}
if (found) {
return this.$inst().$remove(ref.$id);
} else {
return $firebaseUtils.reject('Invalid record; could not find userObject: ' + object);
}
},

/**
* Given an item in this array or the index of an item in the array, this returns the
Expand Down

0 comments on commit 31f5dc8

Please sign in to comment.