-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
60 lines (55 loc) · 2.31 KB
/
helpers.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
// Session helpers
//
// {{#if sessionEquals 'foo' 'bar'}} //where foo is session key containing a value and bar is test value
// {{getSession 'foo'}} //returns session keys value
//
//
(function () {
if (typeof Handlebars !== 'undefined') {
//{{getSession 'key'}}
Handlebars.registerHelper('getSession', function (key) {
return Session.get(key);
});
Handlebars.registerHelper('sessionEquals', function (key, value) {
var myValue = Session.get(key); //Workaround Issue #617
if (typeof(myValue) === 'boolean') //Workaround Issue #617
return Session.equals(key, (value == 'true')); //Workaround Issue #617
return Session.equals(key, (myValue === +myValue)?+value:value); //Workaround Issue #617
//return Session.equals(key, value); //When Issue #617 is resolved
});
Handlebars.registerHelper('findOne', function (collection, query, options) {
//console.log('findOne: '+collection + ' '+query);
var myCollection = window[collection];
if (myCollection instanceof Meteor.Collection){
var myQuery = JSON.parse(query);
var myOptions = (options instanceof Object)?undefined: JSON.parse(options);
//console.log(myCollection.findOne(myQuery));
if (myQuery instanceof Object)
return myCollection.findOne(myQuery, myOptions)
else
console.log('{{findOne}} query error: '+query);
throw new Error('Handlebar helper findOne: "'+collection+'" error in query:'+query+' (remember {"_id":1})');
} else {
throw new Error('Handlebar helper findOne: "'+collection+'" not found');
}
return [];
});
Handlebars.registerHelper('find', function (collection, query, options) {
//console.log('find: '+collection + ' '+query+' '+(options instanceof Object));
var myCollection = window[collection];
if (myCollection instanceof Meteor.Collection){
var myQuery = JSON.parse(query);
var myOptions = (options instanceof Object)?undefined: JSON.parse(options);
//console.log(myCollection.find(myQuery));
if (myQuery instanceof Object)
return myCollection.find(myQuery, myOptions)
else
console.log('{{find}} query error: '+query);
throw new Error('Handlebar helper find: "'+collection+'" error in query:'+query+' (remember {"_id":1})');
} else {
throw new Error('Handlebar helper find: "'+collection+'" not found');
}
return [];
});
}
}());