Created
December 4, 2012 03:21
-
-
Save cmalven/4200232 to your computer and use it in GitHub Desktop.
Meteor Accounts Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Meteor working with accounts | |
# Getting the current user id | |
user = Meteor.userId() | |
# Run some stuff whenever a cursor or session changes | |
Meteor.startup -> | |
Meteor.autorun -> | |
if not Session.get('selected') | |
party = Parties.findOne() | |
if party | |
Session.set('selected', party._id) | |
# Loop through collection and find the owner (requires underscore) | |
myRsvp = _.find this.rsvps, (r) -> | |
return r.user is Meteor.userId() | |
# Allowing | |
Parties = new Meteor.Collection('parties') | |
Parties.allow | |
insert: (userId, party) -> | |
return false # because we want to encourage an another method | |
update: (userId, parties, fields, modifier) -> | |
return _.all parties, (party) -> | |
return false if userId isnt party.owner # not the owner | |
allowed = ['title', 'description'] | |
return false if _.difference fields, allowed.length # tried to write to forbidden field | |
return true | |
remove: (userId, parties) -> | |
return not _any parties, (party) -> | |
# Deny if not the owner | |
return party.owner isnt userId | |
# Methods | |
Meteor.methods | |
createParty: (options) -> | |
# Perform validation on options | |
if not (typeof options.title is 'string' and options.title.length) | |
throw new Meteor.error(400, 'Required parameter missing!') | |
if options.title.length > 100 | |
throw new Meteor.error(413, 'Title is too long!') | |
if not this.userId | |
throw new Meteor.error(403, 'You must be logged in') | |
# If we made it this far, let's insert! | |
return Parties.insert | |
owner: this.userId | |
x: options.x | |
y: options.y | |
title: options.title | |
description: options.description | |
public: !! options.public | |
invited: [] | |
rsvps: [] | |
# Calling these methods | |
Template.details.events | |
'click .some-link': (evt) -> | |
Meteor.call 'createParty', Session.get('selected') | |
evt.preventDefault() | |
# Publishing that belongs to a user | |
Meteor.publish 'parties', -> | |
return Parties.find | |
{$or: [{'public': true}, {invited: this.userId}, {owner: this.userId}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment