流星。 この鉄を傷つける方法:CRUDのルーター?

小学校! しかし、彼らが以前に同様の指示を私に見せたら...

タスク


キーのないリクエスト/プロファイルで 、ユーザーが「間違っている」場合はログインテンプレートを提供し、そうでない場合は、 newProfileを追加するか、 プロファイルのプロファイル (editMode)を編集するテンプレートを提供します。 また、リクエスト/プロファイル/キーに応じて、任意のプロファイルのプロファイル (editModeではない)を表示するテンプレートを提供します。 同時に、userIdは点灯せず、無効なキーはinvalidProfileで拒否されます

[ ソース ]

解決策


コレクションを作成しています
@Collections.Profiles = new Mongo.Collection('profiles') 

ルーターマップ(パス名)。
 Router.route '/profile/:_id?', name: 'profile' 

ルーターを作成します
 @ProfileController = RouteController.extend 

必要なデータを持つ変数(2つのルーター方式間の転送に必要)。
  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 

URLにパラメーターがある場合は、サブスクライブしてコレクションを表示します。
  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'] 


PS削除は実装されていません。 祖父は、何も捨てる必要はなく、農場ですべてが役に立つと教えました。

PSS 1.0のリリース以来、Meteor を楽しんでいます。 いくつかのオープンプロジェクト: ブラウザーグッズニュースフィードフォームジェネレーター携帯電話用フレームワーク、携帯電話用 オンラインストアTrelloクローンメッセージボードチェリャビンスク住民向け教科書crater.ioの流星ニュース。

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


All Articles