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

Commit da83000

Browse files
committed
Merge pull request #215 from mikepugh/master
Add support for transaction method via $transaction
2 parents af7656f + b286567 commit da83000

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ bower_components/
22
node_modules/
33
bower_components/
44
selenium/
5+
.idea

angularfire.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,36 @@
240240
return deferred.promise;
241241
};
242242

243+
// Update a value within a transaction. Calling this is the
244+
// equivalent of calling `transaction()` on a Firebase reference.
245+
//
246+
// * `updateFn`: A developer-supplied function which will be passed the current data
247+
// stored at this location (as a Javascript object). The function should
248+
// return the new value it would like written (as a Javascript object).
249+
// If "undefined" is returned (i.e. you "return;" with no arguments) the
250+
// transaction will be aborted and the data at this location will not be modified.
251+
// * `applyLocally`: By default, events are raised each time the transaction update function runs. So
252+
// if it is run multiple times, you may see intermediate states. You can set this
253+
// to false to suppress these intermediate states and instead wait until the
254+
// transaction has completed before events are raised.
255+
//
256+
// This function returns a promise that will be resolved when the transaction function has completed.
257+
// A successful transaction is resolved with the snapshot. If the transaction is aborted,
258+
// the promise will be resolved with null.
259+
object.$transaction = function(updateFn, applyLocally) {
260+
var deferred = self._q.defer();
261+
self._fRef.ref().transaction(updateFn, function(err, committed, snapshot) {
262+
if(err) {
263+
deferred.reject(err);
264+
} else if(!committed) {
265+
deferred.resolve(null);
266+
} else {
267+
deferred.resolve(snapshot);
268+
}
269+
}, applyLocally);
270+
};
271+
272+
243273
// Remove this object from the remote data. Calling this is the
244274
// equivalent of calling `remove()` on a Firebase reference. This
245275
// function takes a single optional argument:

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/e2e/test_chat.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<span class="content">{{message.content}}</span>
2020
</span>
2121
</div>
22+
<em ng-cloak ng-if="messages" class="messageCountBlock"># Messages: {{messageCount.$value}}</em>
2223
</div>
2324
<form ng-submit="addMessage()">
2425
<input id="message" type="text" ng-model="message" placeholder="Message...">
@@ -27,19 +28,36 @@
2728
<script type="text/javascript">
2829
var _scope;
2930
var _url = 'https://angularfiretests.firebaseio.com/chat';
31+
var _tnUrl = 'https://angularfiretests.firebaseio.com/chatMsgs';
3032
angular.module("chat", ["firebase"]);
3133
function Chat($scope, $firebase) {
3234
$scope.messages = $firebase(new Firebase(_url).limit(2));
35+
$scope.messageCount = $firebase(new Firebase(_tnUrl));
3336
$scope.username = 'Guest' + Math.floor(Math.random()*101);
3437
$scope.addMessage = function() {
3538
var promise = $scope.messages.$add({
3639
from: $scope.username, content: $scope.message
3740
});
41+
42+
// Transaction testing
43+
$scope.messageCount.$transaction(function(currentCount) {
44+
if(!currentCount) {
45+
return 1;
46+
} else {
47+
return currentCount+1;
48+
}
49+
});
50+
3851
$scope.message = "";
3952
return promise;
4053
}
4154
_scope = $scope;
4255
}
56+
57+
function testMessageCount(expectedCount, node) {
58+
return node.innerHTML != '# Messages: ' + expectedCount;
59+
}
60+
4361
function testIfInDOM(from, content, node) {
4462
if (node.childNodes.length != 5) return false;
4563
if (node.childNodes[1].innerHTML != from + ": ") return false;

0 commit comments

Comments
 (0)