Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 248ab36

Browse files
committed
Merge branch 'on-change-callback' of https://github.com/johnsoftek/angularFire into johnsoftek-on-change-callback
2 parents d6cb306 + 0fb4bd6 commit 248ab36

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

angularfire.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ angular.module("firebase").factory("angularFireCollection", ["$timeout",
207207
// argument, and a function as an optional second argument. The callback
208208
// (if provided in the 2nd argument) will be called with a Firebase
209209
// snapshot when the initial data has been loaded.
210-
return function(collectionRef, initialCb) {
210+
return function(collectionRef, initialCb, onItemChangeCb) {
211211
if (typeof collectionRef == "string") {
212212
throw new Error("Please provide a Firebase reference instead " +
213213
"of a URL, eg: new Firebase(url)");
@@ -248,6 +248,12 @@ angular.module("firebase").factory("angularFireCollection", ["$timeout",
248248
var indexes = {};
249249
var collection = [];
250250

251+
function broadcastChange(type, item) {
252+
if (onItemChangeCb && typeof onItemChangeCb == "function") {
253+
onItemChangeCb(type, item);
254+
}
255+
}
256+
251257
function getIndex(prevId) {
252258
return prevId ? indexes[prevId] + 1 : 0;
253259
}
@@ -302,17 +308,21 @@ angular.module("firebase").factory("angularFireCollection", ["$timeout",
302308
collectionRef.on("child_added", function(data, prevId) {
303309
$timeout(function() {
304310
var index = getIndex(prevId);
305-
addChild(index, new AngularFireItem(data, index));
311+
var item = new AngularFireItem(data, index);
312+
addChild(index, item);
306313
updateIndexes(index);
314+
broadcastChange("item_added", item);
307315
});
308316
});
309317

310318
collectionRef.on("child_removed", function(data) {
311319
$timeout(function() {
312320
var id = data.name();
313321
var pos = indexes[id];
322+
var item = collection[pos];
314323
removeChild(id);
315324
updateIndexes(pos);
325+
broadcastChange("item_removed", item);
316326
});
317327
});
318328

@@ -321,11 +331,12 @@ angular.module("firebase").factory("angularFireCollection", ["$timeout",
321331
var index = indexes[data.name()];
322332
var newIndex = getIndex(prevId);
323333
var item = new AngularFireItem(data, index);
324-
334+
broadcastChange("item_removed", collection[index]);
325335
updateChild(index, item);
326336
if (newIndex !== index) {
327337
moveChild(index, newIndex, item);
328338
}
339+
broadcastChange("item_added", item);
329340
});
330341
});
331342

@@ -335,6 +346,7 @@ angular.module("firebase").factory("angularFireCollection", ["$timeout",
335346
var newIndex = getIndex(prevId);
336347
var item = collection[oldIndex];
337348
moveChild(oldIndex, newIndex, item);
349+
broadcastChange("item_moved", item);
338350
});
339351
});
340352

@@ -667,3 +679,4 @@ angular.module("firebase").factory("angularFireAuth", [
667679
};
668680
}
669681
]);
682+

angularfire.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_chat.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
var _url = 'https://angularFireTests.firebaseio-demo.com/chat';
3131
angular.module("chat", ["firebase"]);
3232
function Chat($scope, angularFireCollection) {
33-
$scope.messages = angularFireCollection(new Firebase(_url).limit(2));
33+
34+
function log_item_action(action, item) {
35+
$scope.item_log.push( {action: action, from: item.from, content: item.content} );
36+
}
37+
38+
$scope.item_log = [];
39+
40+
$scope.messages = angularFireCollection(new Firebase(_url).limit(2), null, log_item_action);
3441
$scope.username = 'Guest' + Math.floor(Math.random()*101);
3542
$scope.addMessage = function() {
3643
$scope.messages.add({from: $scope.username, content: $scope.message});

tests/test_chat.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ casper.then(function() {
4141
params[0], params[1], document.querySelector(".messageBlock")
4242
);
4343
}, "Testing if message is in the DOM", [_testName, _testMessage]);
44+
this.test.assertEval(function(params) {
45+
var item = _scope.item_log[0];
46+
return item.action === params[0] &&
47+
item.from === params[1] &&
48+
item.content === params[2];
49+
},
50+
"Testing if callback called", ["item_added", _testName, _testMessage]);
51+
4452
});
4553
});
4654

@@ -66,6 +74,15 @@ casper.then(function() {
6674
}
6775
return testIfInDOM(params[0], params[1], msgs[1]);
6876
}, "Testing if remote message is in the DOM", [_testName, _testMessage]);
77+
78+
this.test.assertEval(function(params) {
79+
var item = _scope.item_log[1];
80+
return item.action === params[0] &&
81+
item.from === params[1] &&
82+
item.content === params[2];
83+
},
84+
"Testing if callback called", ["item_added", _testName, _testMessage]);
85+
6986
});
7087
});
7188

@@ -89,7 +106,19 @@ casper.then(function() {
89106
var msgs = document.querySelectorAll(".messageBlock");
90107
return msgs.length === 2;
91108
}, "Testing if limits and queries work");
92-
});
109+
this.test.assertEval(function() {
110+
var item1 = _scope.item_log[0],
111+
item1x = _scope.item_log[2],
112+
item3 = _scope.item_log[3];
113+
114+
return item1x.action === 'item_removed' &&
115+
item1x.from === item1.from &&
116+
item1x.content === item1.content &&
117+
item3.action === 'item_added' &&
118+
item3.content === "Limit Test";
119+
},
120+
"Testing if callback called for limit test");
121+
});
93122
});
94123

95124
casper.run(function() {

0 commit comments

Comments
 (0)