Vulcan/client/app.js
2012-10-18 10:53:57 +11:00

140 lines
No EOL
3.7 KiB
JavaScript

// ** Client-side helpers **
sessionSetObject=function(name, value){
Session.set(name, JSON.stringify(value));
}
sessionGetObject=function(name){
return JSON.parse(Session.get(name));
}
$.fn.exists = function () {
return this.length !== 0;
}
// ** Errors **
// Local (client-only) collection
Errors = new Meteor.Collection(null);
// ** Settings **
Settings = new Meteor.Collection('settings');
Meteor.subscribe('settings', function(){
// runs once on site load
window.settingsLoaded=true;
if((proxinoKey=getSetting('proxinoKey'))){
Proxino.key = proxinoKey;
Proxino.track_errors();
}
// window.Router = new SimpleRouter();
window.Backbone.history.start({pushState: true});
});
// ** Users **
Meteor.subscribe('users');
// ** Notifications **
// Only load if user is logged in
if(Meteor.user()){
Notifications = new Meteor.Collection('notifications');
Meteor.subscribe('notifications');
}
// ** Posts **
// Collection depends on postsView object
Posts = new Meteor.Collection('posts');
var postsView={
find: {},
sort: {submitted: -1},
skip:0,
postsPerPage:10,
limit:1
}
sessionSetObject('postsView', postsView);
Meteor.autosubscribe(function() {
Session.set('postsReady', false);
var view=sessionGetObject('postsView');
Meteor.subscribe('posts', view, function() {
// collectionArray=Posts.find().fetch();
// console.log('--------- Publishing ----------');
// console.log('postsView: ', view);
// for(i=0;i<collectionArray.length;i++){
// console.log('- '+collectionArray[i].headline);
// }
// console.log('found '+collectionArray.length+' posts');
Session.set('postsReady', true);
});
});
// ** Categories **
Categories = new Meteor.Collection('categories');
Meteor.subscribe('categories');
// ** Comments **
// Collection depends on selectedPostId and selectedCommentId session variable
Session.set('selectedPostId', null);
Comments = new Meteor.Collection('comments');
Meteor.autosubscribe(function() {
var query = { $or : [ { post : Session.get('selectedPostId') } , { _id : Session.get('selectedCommentId') } ] };
Meteor.subscribe('comments', query, function() {
//
});
});
// ** Handlebars helpers **
Handlebars.registerHelper('canView', function(action) {
var action=(typeof action !== 'string') ? null : action;
return canView(Meteor.user(), action);
});
Handlebars.registerHelper('canPost', function(action) {
var action=(typeof action !== 'string') ? null : action;
return canPost(Meteor.user(), action);
});
Handlebars.registerHelper('canComment', function(action) {
var action=(typeof action !== 'string') ? null : action;
return canComment(Meteor.user(), action);
});
Handlebars.registerHelper('canUpvote', function(collection, action) {
var action=(typeof action !== 'string') ? null : action;
return canUpvote(Meteor.user()), collection, action;
});
Handlebars.registerHelper('canDownvote', function(collection, action) {
var action=(typeof action !== 'string') ? null : action;
return canDownvote(Meteor.user(), collection, action);
});
Handlebars.registerHelper('isAdmin', function(showError) {
if(isAdmin(Meteor.user())){
return true;
}else{
if((typeof showError === "string") && (showError === "true"))
throwError('Sorry, you do not have access to this page');
return false;
}
});
Handlebars.registerHelper('canEdit', function(collectionName, action) {
var action = (typeof action !== 'string') ? null : action;
var collection = (typeof collectionName !== 'string') ? Posts : eval(collectionName);
var itemId = (collectionName==="Posts") ? Session.get('selectedPostId') : Session.get('selectedCommentId');
var item=collection.findOne(itemId);
return item && canEdit(Meteor.user(), item, action);
});