Skip to content

Commit

Permalink
Validate movie year in CRUD example
Browse files Browse the repository at this point in the history
  • Loading branch information
dandv committed Aug 13, 2015
1 parent da9e995 commit b2a7987
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion examples/crud/client/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
};
Expand Down
29 changes: 29 additions & 0 deletions examples/crud/server/collection.js
Original file line number Diff line number Diff line change
@@ -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);
}

});

0 comments on commit b2a7987

Please sign in to comment.