Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Jan 14, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.