Skip to content

Commit 925d5f0

Browse files
committed
Adding a $user service binding - thanks to @superchris
1 parent 230d2a2 commit 925d5f0

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ngMeteor
2020
- [Adding controllers, directives, filters and services](https://github.com/urigo/ngmeteor#adding-controllers-directives-filters-and-services)
2121
- [Creating and inserting template views](https://github.com/urigo/ngmeteor#creating-and-inserting-template-views)
2222
- [Routing](https://github.com/urigo/ngmeteor#routing)
23+
- [User service] (https://github.com/urigo/ngmeteor#user)
2324
- [Module Injection](https://github.com/urigo/ngmeteor#module-injection)
2425

2526
### New Data-Binding to avoid conflict
@@ -227,6 +228,23 @@ The [ngRoute](http://docs.angularjs.org/api/ngRoute) module developed by the Ang
227228

228229
For larger applications with more completed routes, it would be wise to consider using the [urigo:angular-ui-router](https://github.com/Urigo/meteor-angular-ui-router) Meteor package for ngMeteor, which exposes the popular [ui-router](https://github.com/angular-ui/ui-router) module to ngMeteor. For those of you that have grown accustomed to the Meteor methods of routing, ngMeteor is compatible with [Iron Router](https://github.com/EventedMind/iron-router).
229230

231+
### User
232+
233+
ngMeteor support a $user service to bind the current logged in user and it's data.
234+
235+
<code>bind</code> - used to bind the current logged in user to your scope:
236+
237+
bind(scope, model)
238+
239+
| Arguments | Type | Description | Required | Default |
240+
| :------------ | :-------- | :------------------------------------------------------------------------ | :-------- | :-------- |
241+
| scope | Scope | The scope the model will be bound to. | Yes | |
242+
| model | String | The scope property the model will be bound to. | Yes | |
243+
244+
$user.bind($scope, 'user');
245+
246+
$user.bind($rootScope, 'user');
247+
230248
### Module Injection
231249
If you have a module called myModule, for example:
232250

modules/ngMeteor-user.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var ngMeteorUser = angular.module('ngMeteor.user', []);
2+
3+
ngMeteorUser.factory("$user", [function() {
4+
return {
5+
bind: function(scope, model) {
6+
Deps.autorun(function(self) {
7+
scope[model] = Meteor.user();
8+
if (!scope.$$phase) scope.$apply();
9+
scope.$on('$destroy', function () {
10+
self.stop(); // Stop computation if scope is destroyed.
11+
});
12+
});
13+
}
14+
}
15+
}]);

package.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Package.on_use(function (api) {
2323
'modules/ngMeteor-subscribe.js',
2424
'modules/ngMeteor-collections.js',
2525
'modules/ngMeteor-template.js',
26+
'modules/ngMeteor-user.js',
2627
// Finally load ngMeteor File
2728
'urigo:ngmeteor.js'
2829
], 'client');

urigo:ngmeteor.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ngMeteor = angular.module('ngMeteor', [
33
'ngMeteor.subscribe',
44
'ngMeteor.collections',
55
'ngMeteor.template',
6+
'ngMeteor.user',
67
'hashKeyCopier'
78
]);
89

0 commit comments

Comments
 (0)