⬆️ ⬇️

Meteor. How to sip this your iron: router for CRUD?

Elementary! But if I had been shown such instructions before ...



The task



On 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 on request / profile / key I give the template to view the profile (not editMode) of any profile; at the same time, userId is not lit, and invalid keys are rejected by invalidProfile .



[ source ]



Decision



I create 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 


Variable with the desired data (needed for transfer between two methods of the router).

  profile: null 


A router will wait for 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 the subscription 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 receive 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 transfer the data to the template for rendering.

  data: -> if not @profile return result = editMode: not @params._id profile: @profile return result 


Publish 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 Uninstall not implemented. The grandfather taught that nothing should be thrown out; everything in the household will come in handy.



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

Source: https://habr.com/ru/post/247849/



All Articles