@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']
Source: https://habr.com/ru/post/247849/