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

Commit d6cbc19

Browse files
committed
Use explicit dependency declaration to protect from minification, tweak
promise explanation in README
1 parent 5323c71 commit d6cbc19

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ will instantly update the local model.
3838
Set `angularFire` as a service dependency in your controller:
3939

4040
```js
41-
myapp.controller('MyCtrl', function MyCtrl($scope, angularFire) {
42-
...
43-
});
41+
myapp.controller('MyCtrl', ['$scope', 'angularFire',
42+
function MyCtrl($scope, angularFire) {
43+
...
44+
}
45+
]);
4446
```
4547

4648
Bind a Firebase URL to any model in `$scope`:
@@ -58,13 +60,23 @@ Use the model in your markup as you normally would:
5860
</ul>
5961
```
6062

61-
To add or remove items, simply edit the model (a JavaScript array by default)
62-
after the promise has completed:
63+
Data from Firebase is loaded asynchronously, so make sure you edit the model
64+
*only after the promise has been fulfilled*. You can do this using the `then`
65+
method (See the
66+
[Angular documentation on $q](http://docs.angularjs.org/api/ng.$q)
67+
for more no how promises work).
68+
69+
Place all your logic that will manipulate the model like this:
6370

6471
```js
6572
$scope.items.then(function() {
66-
// Add a new item by simply modifying the array.
73+
// Add a new item by simply modifying the model directly.
6774
$scope.items.push({name: "Firebase", desc: "is awesome!"});
75+
// Or, attach a function to $scope that will let a directive in markup manipulate the model.
76+
$scope.removeItem = function() {
77+
$scope.items.splice($scope.toRemove, 1);
78+
$scope.toRemove = null;
79+
};
6880
});
6981
```
7082

examples/chat/app.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11

22
angular.module('chat', ['firebase']).
3-
controller('Chat', function($scope, $timeout, angularFireCollection) {
4-
var el = document.getElementById("messagesDiv");
5-
var url = 'https://angularFire.firebaseio-demo.com/chat';
6-
$scope.messages = angularFireCollection(url, function() {
7-
$timeout(function() { el.scrollTop = el.scrollHeight; });
8-
});
9-
$scope.username = 'Guest' + Math.floor(Math.random()*101);
10-
$scope.addMessage = function() {
11-
$scope.messages.$add({from: $scope.username, content: $scope.message}, function() {
12-
el.scrollTop = el.scrollHeight;
3+
controller('Chat', ['$scope', '$timeout', 'angularFireCollection',
4+
function($scope, $timeout, angularFireCollection) {
5+
var el = document.getElementById("messagesDiv");
6+
var url = 'https://angularFire.firebaseio-demo.com/chat';
7+
$scope.messages = angularFireCollection(url, function() {
8+
$timeout(function() { el.scrollTop = el.scrollHeight; });
139
});
14-
$scope.message = "";
10+
$scope.username = 'Guest' + Math.floor(Math.random()*101);
11+
$scope.addMessage = function() {
12+
$scope.messages.$add({from: $scope.username, content: $scope.message}, function() {
13+
el.scrollTop = el.scrollHeight;
14+
});
15+
$scope.message = "";
16+
}
1517
}
16-
});
18+
]);

examples/todomvc/js/controllers/todoCtrl.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
* - retrieves and persist the model via the todoStorage service
77
* - exposes the model to the template and provides event handlers
88
*/
9-
todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, angularFire, filterFilter) {
10-
var url = "https://angularFire.firebaseio-demo.com/todomvc";
11-
$scope.todos = angularFire(url, $scope, 'todos');
9+
todomvc.controller('TodoCtrl', ['$scope', '$location', 'angularFire', 'filterFilter',
10+
function TodoCtrl($scope, $location, angularFire, filterFilter) {
11+
var url = "https://angularFire.firebaseio-demo.com/todomvc";
12+
$scope.todos = angularFire(url, $scope, 'todos');
1213

13-
$scope.newTodo = '';
14-
$scope.editedTodo = null;
14+
$scope.newTodo = '';
15+
$scope.editedTodo = null;
1516

16-
if ($location.path() === '') {
17-
$location.path('/');
18-
}
19-
$scope.location = $location;
17+
if ($location.path() === '') {
18+
$location.path('/');
19+
}
20+
$scope.location = $location;
2021

21-
$scope.todos.then(function(todos) {
22-
startWatch($scope, filterFilter);
23-
});
24-
});
22+
$scope.todos.then(function(todos) {
23+
startWatch($scope, filterFilter);
24+
});
25+
}
26+
]);
2527

2628
function startWatch($scope, filter) {
2729
$scope.$watch('todos', function () {

0 commit comments

Comments
 (0)