Skip to content

Instantly share code, notes, and snippets.

@themeteorchef
Created May 19, 2015 22:08
Show Gist options
  • Save themeteorchef/194a803f8a28840f475f to your computer and use it in GitHub Desktop.
Save themeteorchef/194a803f8a28840f475f to your computer and use it in GitHub Desktop.
Meteor Method Pattern
// On the client-side.
Template.exampleTemplate.events({
'submit form': function(e, t) {
var dataToAddToDatabase = t.find('input.example').value;
// This will work just fine, but delegates the insert to the server.
Meteor.call('processFormData', dataToAddToDatabase, function(error, response){
// Do something with feedback.
});
// This would fail, throwing an error in the console. Even if I call this *from* the console, it fails.
ExampleCollection.insert({key: dataToAddToDatabase});
}
});
ExampleCollection = new Meteor.Collection("example-collection");
ExampleCollection.allow({
insert: function() {
// Deny all client-side inserts.
return false;
},
update: function() {
// Deny all client-side updates.
return false;
},
remove: function() {
// Deny all client-side removes.
return false;
}
});
ExampleCollection.deny({
insert: function() {
// Deny all client-side inserts.
return true;
},
update: function() {
// Deny all client-side updates.
return true;
},
remove: function() {
// Deny all client-side removes.
return true;
}
});
// On the server-side.
Meteor.methods({
processFormData: function( dataToAddToDatabase ) {
ExampleCollection.insert({key: dataToAddToDatabase});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment