Skip to content

Commit

Permalink
Add the $methods service - Wraps Meteor.methods with AngularJS promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Urigo committed Oct 12, 2014
1 parent 10fbe4f commit cbb273b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [Creating and inserting template views](https://github.com/urigo/angular-meteor#creating-and-inserting-template-views)
- [Routing](https://github.com/urigo/angular-meteor#routing)
- [User service] (https://github.com/urigo/angular-meteor#User)
- [Meteor methods with promises] (https://github.com/urigo/angular-meteor#meteor-methods-with-promises)

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

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

### Meteor methods with promises

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).

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:

$scope.invite = function(party, user){

$methods.call('invite', party._id, user._id).then(
function(data){
// Handle success
console.log('success inviting', data.userId);
},
function(err){
// Handle error
console.log('failed', err);
}
);
};

### Additional packages

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:
Expand Down
20 changes: 20 additions & 0 deletions modules/angular-meteor-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
var angularMeteorMethods = angular.module('angular-meteor.methods', []);

angularMeteorMethods.service('$methods', ['$q',
function ($q) {
this.call = function(){

var deferred = $q.defer();

[].push.call(arguments, function (err, data) {
if (err)
deferred.reject(err);
else
deferred.resolve(data);
});
Meteor.call.apply(this, arguments);

return deferred.promise;
};
}]);
1 change: 1 addition & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Package.on_use(function (api) {
'modules/angular-meteor-collections.js',
'modules/angular-meteor-template.js',
'modules/angular-meteor-user.js',
'modules/angular-meteor-methods.js',
// Finally load angular-meteor File
'urigo:angular.js'
], 'client');
Expand Down
1 change: 1 addition & 0 deletions urigo:angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ angularMeteor = angular.module('angular-meteor', [
'angular-meteor.collections',
'angular-meteor.template',
'angular-meteor.user',
'angular-meteor.methods',
'hashKeyCopier'
]);

Expand Down

0 comments on commit cbb273b

Please sign in to comment.