I always use methods to insert, update and delete. This is what my code looks like now:
Client side
Template.createClient.events({
'submit form': function(event, tmpl) {
e.preventDefault();
var client = {
name: event.target.name.value,
}
var validatedData = Clients.validate(client);
if (validatedData.errors) {
return;
}
Meteor.call('createClient', validatedData.client, function(error) {
if (error)
});
}
});
Client and server side:
Clients = new Mongo.Collection("clients");
Clients.validate = function(client) {
client.name = _.str.trim(client.name);
var errors = [];
if (!client.name)
errors.push("The name is required.");
return { errors: _.isEmpty(errors) ? undefined : errors, client: client };
}
Meteor.methods({
'createClient': function (client) {
if (Meteor.isServer) {
var validatedData = Clients.validate(client);
if (validatedData.errors)
throw new Meteor.Error(500, "Invalid client.");
client = validatedData.client;
}
check(client, {
name: String,
});
return Clients.insert(client);
}
});
Meteor.call is executed on the client and server side, but Meteor does not have the ability to stop working on the server side if the client-side check failed (or at least I don’t know how to do this). With this template, I avoid sending data to the server using Meteor.call if validation fails.
I want to start using Collection2, but I cannot figure out how to get the same template. All the examples I found include using direct insertion and client-side updates and Allow / Deny for security management, but I want to stick with Meteor.call.
, , , , :
Books.simpleSchema().namedContext().validate({title: "Ulysses", author: "James Joyce"}, {modifier: false});
autoform, .
Collection2 Meteor.call? Collection2, -?