Meteor How to gash this your iron: router for CRUD?

  • Tutorial
Elementary! But if they showed me a similar instruction before ...

The task


At the request / profile without a key I give the login template if the user is “wrong”, otherwise I give the template to add newProfile or edit the profile (editMode) of my profile. And upon request / profile / key I give the template to view the profile (not editMode) of any profile; at the same time userId does not light up, and invalid keys are rejected on invalidProfile .

[ source ]

Decision


I am creating a collection .
@Collections.Profiles = new Mongo.Collection('profiles')

Router map (path name).
Router.route '/profile/:_id?', 
  name: 'profile'

I create a router .
@ProfileController = RouteController.extend

A variable with the required data (needed for transfer between two router methods).
  profile: null

The router will expect announced subscriptions .
  waitOn: ->

Magic is reactivity .
    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

If the URL has a parameter, then subscribe to view the collection.
    else if @params._id
      return Meteor.subscribe 'profile4view', @params._id

Otherwise, if the user is logged in, then a subscription to edit the collection.
    else if Meteor.userId()
      return Meteor.subscribe 'profile4edit', Meteor.userId()

Processing a request to the router: I get data from the collection and select a template.
  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()

I pass the data to the template for rendering.
  data: ->
    if not @profile
      return
    result = 
      editMode: not @params._id
      profile: @profile
    return result

Publishing Subscriptions .
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
  ]

Access rules for the collection.
@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']


PS Removal is not implemented. Grandfather taught that you don’t have to throw anything away, everything will come in handy on the farm.

PSS Having fun on the Meteor since the release of 1.0, I highly recommend it. Several open projects: a browser toy , news feed , a form generator , a framework for mobile phones , an online store for mobile phones , a dude , a Trello clone , a message board , a textbook for Chelyabinsk residents . Meteor news at crater.io .

Also popular now: