-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortable-autoform-example.js
75 lines (66 loc) · 1.54 KB
/
sortable-autoform-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
if (Meteor.isClient) {
Template.example.helpers({
exampleOptions: function() {
return {
sort: true,
group: {
scroll: true,
name: "exampleSortGroup",
pull: true
}
}
},
exampleItems: function() {
return ExampleItems.find({},{sort: {order: 1}});
}
});
Template.item.helpers({
itemFormSchema: function() {
return Schema.exampleItem;
},
formId: function() {
return 'formExampleItem' + this._id;
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
if(ExampleItems.find().count() == 0){
ExampleItems.insert({title: "Test A", order: 1});
ExampleItems.insert({title: "Test B", order: 2});
ExampleItems.insert({title: "Test C", order: 3});
}
});
}
// Init DB collection
ExampleItems = new Mongo.Collection('exampleItems');
// Simple collection schema
Schema = {};
Schema.exampleItem = new SimpleSchema({
title: {
type: String,
label: "Title",
max: 200
},
order: {
type: Number,
label: "Sorting",
optional: true,
autoform: {
omit: true
},
autoValue: function() {
if (this.isInsert) {
var lastSortIndex = ExampleItems.find({}, {sort: {order: -1}, limit: 1});
if(lastSortIndex.count() > 0){
return lastSortIndex.fetch()[0].order + 1;
}
return 1;
} else {
return this.value;
}
}
}
});
ExampleItems.attachSchema(Schema.exampleItem);