Skip to content

Commit cbb273b

Browse files
committed
Add the $methods service - Wraps Meteor.methods with AngularJS promises
1 parent 10fbe4f commit cbb273b

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Creating and inserting template views](https://github.com/urigo/angular-meteor#creating-and-inserting-template-views)
2323
- [Routing](https://github.com/urigo/angular-meteor#routing)
2424
- [User service] (https://github.com/urigo/angular-meteor#User)
25+
- [Meteor methods with promises] (https://github.com/urigo/angular-meteor#meteor-methods-with-promises)
2526

2627
### App initialization
2728
If you have a module called myModule, for example:
@@ -260,6 +261,27 @@ angular-meteor support a $user service to bind the current logged in user and it
260261

261262
[More in step 8 of the tutorial](http://angularjs.meteor.com/tutorial-02/step_08)
262263

264+
### Meteor methods with promises
265+
266+
angular-meteor introduces the $methods service with wraps up [Meteor.methods](http://docs.meteor.com/#methods_header) with [AngularJS promises](https://docs.angularjs.org/api/ng/service/$q).
267+
268+
Simply call **$methods.call** function and instead of sending the function of handling success and error as a parameter, handle the success and error in the AngularJS way with the 'then' method:
269+
270+
$scope.invite = function(party, user){
271+
272+
$methods.call('invite', party._id, user._id).then(
273+
function(data){
274+
// Handle success
275+
console.log('success inviting', data.userId);
276+
},
277+
function(err){
278+
// Handle error
279+
console.log('failed', err);
280+
}
281+
);
282+
283+
};
284+
263285
### Additional packages
264286

265287
Using this method, additional functionality has been provided to urigo:angular-meteor in the form of separate Meteor packages that expose and inject angular modules into angular-meteor. These packages have been developed by either the angular-meteor Team and/or by third parties. The following is a non-exhaustive list of these packages:

modules/angular-meteor-methods.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
var angularMeteorMethods = angular.module('angular-meteor.methods', []);
3+
4+
angularMeteorMethods.service('$methods', ['$q',
5+
function ($q) {
6+
this.call = function(){
7+
8+
var deferred = $q.defer();
9+
10+
[].push.call(arguments, function (err, data) {
11+
if (err)
12+
deferred.reject(err);
13+
else
14+
deferred.resolve(data);
15+
});
16+
Meteor.call.apply(this, arguments);
17+
18+
return deferred.promise;
19+
};
20+
}]);

package.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Package.on_use(function (api) {
2424
'modules/angular-meteor-collections.js',
2525
'modules/angular-meteor-template.js',
2626
'modules/angular-meteor-user.js',
27+
'modules/angular-meteor-methods.js',
2728
// Finally load angular-meteor File
2829
'urigo:angular.js'
2930
], 'client');

urigo:angular.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ angularMeteor = angular.module('angular-meteor', [
44
'angular-meteor.collections',
55
'angular-meteor.template',
66
'angular-meteor.user',
7+
'angular-meteor.methods',
78
'hashKeyCopier'
89
]);
910

0 commit comments

Comments
 (0)