2014-05-25 19:49:32 +02:00
|
|
|
notes = new Meteor.Collection "notes"
|
2014-05-25 12:01:38 +02:00
|
|
|
|
|
|
|
if Meteor.isServer
|
2014-05-25 19:49:32 +02:00
|
|
|
#notes.insert { content: "Example" } unless notes.find().fetch().length > 0
|
2014-05-25 14:17:55 +02:00
|
|
|
# Accounts
|
|
|
|
###
|
|
|
|
Accounts.registerLoginHandler (req) ->
|
|
|
|
return null unless req.mail and req.password
|
|
|
|
return null unless req.mail.length > 4 and req.pass.length >= 8
|
|
|
|
user = Meteor.users.findOne { mail: req.mail }
|
|
|
|
if not user
|
|
|
|
user = Meteor.insert { mail: req.mail, password: req.password }
|
|
|
|
{ id: user._id }
|
|
|
|
###
|
|
|
|
|
|
|
|
Accounts.config {
|
|
|
|
sendVerificationEmail: true
|
|
|
|
loginExpirationInDays: 1
|
|
|
|
}
|
|
|
|
|
2014-05-25 19:49:32 +02:00
|
|
|
Meteor.publish "my-notes", ->
|
|
|
|
notes.find( { userId: @userId } ) unless not @userId
|
2014-05-25 12:01:38 +02:00
|
|
|
|
|
|
|
if Meteor.isClient
|
2014-05-25 19:49:32 +02:00
|
|
|
Meteor.subscribe "my-notes"
|
2014-05-25 14:17:55 +02:00
|
|
|
# Notes template
|
2014-05-25 12:01:38 +02:00
|
|
|
Template.notes.notes = ->
|
2014-05-25 19:49:32 +02:00
|
|
|
notes.find().fetch()
|
2014-05-25 12:01:38 +02:00
|
|
|
Template.notes.events {
|
2014-05-26 12:08:14 +02:00
|
|
|
'click .delete': -> notes.remove @_id
|
|
|
|
'click .edit': ->
|
|
|
|
Template.edit.note = this; console.log Template.edit.note
|
|
|
|
UI.render Template.edit
|
|
|
|
}
|
|
|
|
|
|
|
|
# Note Editor TODO: Make Reactive
|
|
|
|
Template.edit.show = ->
|
|
|
|
console.log Template.edit.note isnt undefined
|
|
|
|
Template.edit.note isnt undefined
|
|
|
|
Template.edit.note = undefined
|
|
|
|
Template.edit.events {
|
|
|
|
'click .close': -> Template.edit.note = undefined
|
|
|
|
'click .save': -> null
|
2014-05-25 12:01:38 +02:00
|
|
|
}
|
2014-05-25 19:49:32 +02:00
|
|
|
|
|
|
|
# Auth
|
|
|
|
Template.auth.alerts = []
|
|
|
|
Template.auth.errCallback = (err) ->
|
|
|
|
Template.auth.alert { msg: err.reason }
|
|
|
|
|
2014-05-26 12:08:14 +02:00
|
|
|
# TODO: make reactive
|
2014-05-25 19:49:32 +02:00
|
|
|
Template.auth.alert = (add,remove) ->
|
2014-05-26 12:08:14 +02:00
|
|
|
if add then Template.auth.alerts.push add
|
2014-05-25 19:49:32 +02:00
|
|
|
if remove
|
|
|
|
Template.auth.alerts.splice Template.auth.alerts.indexOf(remove), 1
|
|
|
|
Template.auth.alerts
|
|
|
|
|
2014-05-25 14:17:55 +02:00
|
|
|
Template.auth.events {
|
2014-05-25 19:49:32 +02:00
|
|
|
'click .delete': (e,template) -> Template.auth.alert null, this
|
2014-05-25 14:17:55 +02:00
|
|
|
'keypress .login': (e,template) ->
|
2014-05-25 19:49:32 +02:00
|
|
|
mail = template.find('#mail').value; pass = template.find('#pass').value
|
|
|
|
if e.keyCode is 13 # Login
|
|
|
|
Meteor.loginWithPassword mail, pass, Template.auth.errCallback
|
|
|
|
# Login
|
2014-05-25 14:17:55 +02:00
|
|
|
'click #login': (e,template) ->
|
|
|
|
mail = template.find('#mail').value; pass = template.find('#pass').value
|
2014-05-25 19:49:32 +02:00
|
|
|
Meteor.loginWithPassword mail, pass, Template.auth.errCallback
|
|
|
|
# Register
|
2014-05-25 14:17:55 +02:00
|
|
|
'click #register': (e,template) ->
|
|
|
|
mail = template.find('#mail').value; pass = template.find('#pass').value
|
2014-05-25 19:49:32 +02:00
|
|
|
Accounts.createUser { email: mail, password: pass }, Template.auth.errCallback
|
2014-05-25 14:17:55 +02:00
|
|
|
}
|
|
|
|
# User Logged In
|
|
|
|
Template.userInfo.events {
|
|
|
|
'click #logout': (e,template) ->
|
|
|
|
Meteor.logout()
|
2014-05-25 19:49:32 +02:00
|
|
|
'keypress #newNote': (e,template) ->
|
|
|
|
if e.keyCode is 13
|
|
|
|
notes.insert {
|
|
|
|
content: template.find('#newNote').value
|
|
|
|
userId: Meteor.userId()
|
|
|
|
}
|
|
|
|
template.find('#newNote').value = ""
|
2014-05-25 14:17:55 +02:00
|
|
|
}
|
2014-05-25 19:49:32 +02:00
|
|
|
Template.userInfo.in = -> Meteor.user().emails[0].address
|