Skip to content

Instantly share code, notes, and snippets.

@comerc
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comerc/463edb3cf1527942a25a to your computer and use it in GitHub Desktop.
Save comerc/463edb3cf1527942a25a to your computer and use it in GitHub Desktop.
How to use iron:router for CRUD
@Collections.Profiles = new Mongo.Collection('profiles')
Router.route '/profile/:_id?',
name: 'profile'
@ProfileController = RouteController.extend
profile: null
waitOn: ->
if Meteor.loggingIn()
# First, because twice exec. It is reactivity: http://docs.meteor.com/#/full/reactivity
# https://github.com/EventedMind/iron-router/blob/devel/Guide.md#reactivity
else if @params._id
return Meteor.subscribe 'profile4view', @params._id
else if Meteor.userId()
return Meteor.subscribe 'profile4edit', Meteor.userId()
action: ->
@profile = null
if Meteor.loggingIn()
@template = 'wait'
else if @params._id
@profile = Collections.Profiles.findOne
_id: @params._id
if @profile
@template = 'profile'
else
@template = 'invalidProfile'
else if Meteor.userId()
@profile = Collections.Profiles.findOne
userId: Meteor.userId()
if @profile
@template = 'profile'
else
@template = 'newProfile'
else
@template = 'login'
@render()
data: ->
if not @profile
return
result =
editMode: not @params._id
profile: @profile
return result
Meteor.publish 'profile4edit', (userId) ->
check(arguments, [Match.Any])
[
Collections.Profiles.find
userId: userId
]
Meteor.publish 'profile4view', (id) ->
check(arguments, [Match.Any])
[
Collections.Profiles.find
_id: id
,
fields:
userId: 0
]
@Collections.Profiles.allow
insert: (userId, doc) ->
userId and doc and userId is doc.userId
update: (userId, doc, fieldNames, modifier) ->
userId and doc and userId is doc.userId
# remove: (userId, doc) ->
# userId and doc and userId is doc.userId
fetch: ['userId']
@comerc
Copy link
Author

comerc commented Jan 14, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment