Skip to content

Commit 6a060b4

Browse files
committed
Adding $session service, closes #51
1 parent 58d2a33 commit 6a060b4

File tree

9 files changed

+142
-4
lines changed

9 files changed

+142
-4
lines changed

.docs/angular-meteor/client/scripts/routes.js

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ angular.module("meteor-angular-docs").config(['$urlRouterProvider', '$stateProvi
3737
url: '/subscribe',
3838
template: UiRouter.template('api.subscribe.html')
3939
})
40+
.state('api.session', {
41+
url: '/session',
42+
template: UiRouter.template('api.session.html')
43+
})
4044
.state('api.methods', {
4145
url: '/methods',
4246
template: UiRouter.template('api.methods.html')

.docs/angular-meteor/client/views/api.html

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ <h2><a href="/api">API</a></h2>
99
<li><a href="/api/collection-bindOne">$collection.bindOne</a></li>
1010
<li><a href="/api/AngularMeteorCollection">AngularMeteorCollection</a></li>
1111
<li><a href="/api/subscribe">$subscribe.subscribe</a></li>
12+
<li><a href="/api/session">$session.bind</a></li>
1213
<li><a href="/api/methods">$methods.call</a></li>
1314
<li><a href="/api/user">User</a></li>
1415
<li><a href="/api/meteor-include">meteor-include directive</a></li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<template name="api.session.html">
2+
<div>
3+
<a href="https://github.com/Urigo/angular-meteor/edit/master/.docs/angular-meteor/client/views/api/api.session.html"
4+
class="btn btn-default btn-lg improve-button">
5+
<i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this doc
6+
</a>
7+
8+
<do-nothing>
9+
{{#markdown}}
10+
11+
# $session
12+
13+
A service that binds a scope variable to a Meteor Session variable.
14+
15+
----
16+
17+
## Usage
18+
19+
$session(sessionKey).bind(scope, model)
20+
21+
### Arguments
22+
23+
<table class="variables-matrix input-arguments">
24+
<thead>
25+
<tr>
26+
<th>Param</th>
27+
<th>Type</th>
28+
<th>Details</th>
29+
<th>Required</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
<tr>
34+
<td>sessionKey</td>
35+
<td><a href="https://docs.angularjs.org/guide/scope" class="label type-hint type-hint-string">string</a></td>
36+
<td><p>The name of the session variable</p></td>
37+
<td><a href="" class="label type-hint type-hint-array">Yes</a></td>
38+
</tr>
39+
<tr>
40+
<td>scope</td>
41+
<td><a href="https://docs.angularjs.org/guide/scope" class="label type-hint type-hint-regexp">Scope</a></td>
42+
<td><p>The scope the document will be bound to</p></td>
43+
<td><a href="" class="label type-hint type-hint-array">Yes</a></td>
44+
</tr>
45+
<tr>
46+
<td>model</td>
47+
<td><a href="" class="label type-hint type-hint-string">String</a></td>
48+
<td><p>The name of the scope's model variable that the document will be bound to</p></td>
49+
<td><a href="" class="label type-hint type-hint-array">Yes</a></td>
50+
</tr>
51+
</tbody>
52+
</table>
53+
54+
----
55+
56+
## Example
57+
58+
if (Meteor.isClient) {
59+
60+
app.controller("detailCtrl", ['$scope', '$session',
61+
function($scope, $session){
62+
63+
$session('counter').bind($scope, 'counter');
64+
65+
Tracker.autorun(function () {
66+
console.log('angular counter changed', Session.get('counter'));
67+
});
68+
69+
}]);
70+
71+
}
72+
73+
Template.hello.events({
74+
'click button': function () {
75+
// increment the counter when button is clicked
76+
Session.set("counter", Session.get("counter") + 1);
77+
}
78+
});
79+
80+
}
81+
82+
HTML
83+
84+
<div ng-controller="MainCtrl">
85+
angular counter = [[ counter ]]
86+
<button ng-click="counter=counter+1">Add angular counter</button>
87+
<button>Add Meteor counter</button>
88+
</div>
89+
90+
{{/markdown}}
91+
</do-nothing>
92+
93+
</div>
94+
</template>

.docs/angular-meteor/public/sitemap.xml

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@
101101
<loc>http://angularjs.meteor.com/api/subscribe</loc>
102102
<priority>0.5</priority>
103103
</url>
104+
<url>
105+
<loc>http://angularjs.meteor.com/api/session</loc>
106+
<priority>0.5</priority>
107+
</url>
104108
<url>
105109
<loc>http://angularjs.meteor.com/api/methods</loc>
106110
<priority>0.5</priority>

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
- [Adding controllers, directives, filters and services](https://github.com/urigo/angular-meteor#adding-controllers-directives-filters-and-services)
3131
- [Creating and inserting template views](https://github.com/urigo/angular-meteor#creating-and-inserting-template-views)
3232
- [Routing](https://github.com/urigo/angular-meteor#routing)
33-
- [User service] (https://github.com/urigo/angular-meteor#User)
34-
- [Meteor methods with promises] (https://github.com/urigo/angular-meteor#meteor-methods-with-promises)
33+
- [User service](https://github.com/urigo/angular-meteor#User)
34+
- [Meteor methods with promises](https://github.com/urigo/angular-meteor#meteor-methods-with-promises)
35+
- [Bind Meteor session](https://github.com/urigo/angular-meteor#bind-meteor-session)
3536

3637
### App initialization
3738
If you have a module called myModule, for example:
@@ -142,6 +143,10 @@ It would be wise to consider using the [urigo:angular-ui-router](https://github.
142143
### Meteor methods with promises
143144

144145
[$methods](http://angularjs.meteor.com/api/methods)
146+
147+
### Bind Meteor session
148+
149+
[$session](http://angularjs.meteor.com/api/session)
145150

146151
### Additional packages
147152

modules/angular-meteor-session.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
var angularMeteorSession = angular.module('angular-meteor.session', []);
3+
4+
angularMeteorSession.factory('$session', [
5+
function () {
6+
return function (session) {
7+
8+
return {
9+
10+
bind: function(scope, model) {
11+
Tracker.autorun(function(self) {
12+
scope[model] = Session.get(session);
13+
if (!scope.$root.$$phase) scope.$apply(); // Update bindings in scope.
14+
scope.$on('$destroy', function () {
15+
self.stop(); // Stop computation if scope is destroyed.
16+
});
17+
});
18+
19+
scope.$watch(model, function (newItem, oldItem) {
20+
Session.set(session, scope[model]);
21+
}, true);
22+
23+
}
24+
};
25+
}
26+
}
27+
]);
28+

package.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
summary: "The simplest no-conflict way to use AngularJS with Meteor, Meteorite and Atmosphere Smart Packages.",
3-
version: "0.5.6",
3+
version: "0.5.7",
44
git: "https://github.com/Urigo/angular-meteor.git"
55
});
66

@@ -25,6 +25,7 @@ Package.on_use(function (api) {
2525
'modules/angular-meteor-template.js',
2626
'modules/angular-meteor-user.js',
2727
'modules/angular-meteor-methods.js',
28+
'modules/angular-meteor-session.js',
2829
// Finally load angular-meteor File
2930
'urigo:angular.js'
3031
], 'client');

smart.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"homepage": "https://github.com/Urigo/angular-meteor.git",
55
"author": "Androo Lee & Uri Goldshtein",
66
"git": "https://github.com/Urigo/angular-meteor.git",
7-
"version": "0.5.6",
7+
"version": "0.5.7",
88
"packages": {
99
"bower": {}
1010
},

urigo:angular.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ angularMeteor = angular.module('angular-meteor', [
55
'angular-meteor.template',
66
'angular-meteor.user',
77
'angular-meteor.methods',
8+
'angular-meteor.session',
89
'hashKeyCopier'
910
]);
1011

0 commit comments

Comments
 (0)