From b2a79879dfa66a782028215c166ad17ce8f97f04 Mon Sep 17 00:00:00 2001 From: Dan Dascalescu Date: Thu, 13 Aug 2015 06:18:42 -0700 Subject: [PATCH] Validate movie year in CRUD example --- examples/crud/client/crud.js | 8 +++++++- examples/crud/server/collection.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 examples/crud/server/collection.js diff --git a/examples/crud/client/crud.js b/examples/crud/client/crud.js index 56ca295..f2bba6b 100644 --- a/examples/crud/client/crud.js +++ b/examples/crud/client/crud.js @@ -5,7 +5,7 @@ var dataTable = { view: 'datatable', id: 'datatable', // Columns can usually be omitted and they can be automatically detected with autoconfig: true - // But since we don't know what data in the DB might confused the autodetection, we'll specify: + // But since we don't know what data in the DB might confuse the autodetection, we'll specify: columns: [ // http://docs.webix.com/api__ui.datatable_columns_config.html { id: 'title', header: 'Title', editor: 'text', fillspace: true }, // fill remaining width in the table @@ -17,6 +17,12 @@ var dataTable = { editable: true, // redundant, but see http://forum.webix.com/discussion/4328/editable-true-doesn-t-do-anything-if-columns-don-t-have-editor-specified editaction: 'dblclick', resizeColumn: true, + // validation + rules: { + 'year': function (value) { + return value > 1890 && value < 2030 + } + }, url: webix.proxy('meteor', Movies), // <-- this is it! save: webix.proxy('meteor', Movies) // Mongo.Collection }; diff --git a/examples/crud/server/collection.js b/examples/crud/server/collection.js new file mode 100644 index 0000000..a85d216 --- /dev/null +++ b/examples/crud/server/collection.js @@ -0,0 +1,29 @@ +function isAdmin(userId) { + return userId === 'YTBKpxPLzEoFjsGy6'; // dandv's userId +} + + +// hack to transform documents before insertion - https://github.com/oortcloud/unofficial-meteor-faq/blob/master/README.md#user-content-how-can-i-alter-every-document-before-it-is-added-to-the-database + +Movies.deny({ + insert: function (userId, doc) { + doc.createdAt = new Date(); + doc.creator = userId; + return false; + } +}); + +Movies.allow({ + insert: function (userId, doc) { + // anybody can insert + return true; + }, + update: function (userId, doc, fields, modifier) { + return userId && doc.userId === userId || isAdmin(userId); + }, + remove: function (userId, doc) { + // can only remove your own documents + return userId && doc.userId === userId || isAdmin(userId); + } + +});