Skip to content

Commit b2a7987

Browse files
committed
Validate movie year in CRUD example
1 parent da9e995 commit b2a7987

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

examples/crud/client/crud.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var dataTable = {
55
view: 'datatable',
66
id: 'datatable',
77
// Columns can usually be omitted and they can be automatically detected with autoconfig: true
8-
// But since we don't know what data in the DB might confused the autodetection, we'll specify:
8+
// But since we don't know what data in the DB might confuse the autodetection, we'll specify:
99
columns: [
1010
// http://docs.webix.com/api__ui.datatable_columns_config.html
1111
{ id: 'title', header: 'Title', editor: 'text', fillspace: true }, // fill remaining width in the table
@@ -17,6 +17,12 @@ var dataTable = {
1717
editable: true, // redundant, but see http://forum.webix.com/discussion/4328/editable-true-doesn-t-do-anything-if-columns-don-t-have-editor-specified
1818
editaction: 'dblclick',
1919
resizeColumn: true,
20+
// validation
21+
rules: {
22+
'year': function (value) {
23+
return value > 1890 && value < 2030
24+
}
25+
},
2026
url: webix.proxy('meteor', Movies), // <-- this is it!
2127
save: webix.proxy('meteor', Movies) // Mongo.Collection
2228
};

examples/crud/server/collection.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function isAdmin(userId) {
2+
return userId === 'YTBKpxPLzEoFjsGy6'; // dandv's userId
3+
}
4+
5+
6+
// 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
7+
8+
Movies.deny({
9+
insert: function (userId, doc) {
10+
doc.createdAt = new Date();
11+
doc.creator = userId;
12+
return false;
13+
}
14+
});
15+
16+
Movies.allow({
17+
insert: function (userId, doc) {
18+
// anybody can insert
19+
return true;
20+
},
21+
update: function (userId, doc, fields, modifier) {
22+
return userId && doc.userId === userId || isAdmin(userId);
23+
},
24+
remove: function (userId, doc) {
25+
// can only remove your own documents
26+
return userId && doc.userId === userId || isAdmin(userId);
27+
}
28+
29+
});

0 commit comments

Comments
 (0)