Skip to content

Latest commit

Β 

History

History
70 lines (56 loc) Β· 3.45 KB

File metadata and controls

70 lines (56 loc) Β· 3.45 KB

μƒˆλ‘œμš΄ νŽ˜μ΄μ§€ 생성 / 경둜 μ—°κ²°

λΈŒλΌμš°μ € μ£Όμ†Œμ°½μ— http://localhost:3000 을 μž…λ ₯ ν•˜λ©΄ λΈŒλΌμš°μ €λŠ” ν•΄λ‹Ή μ£Όμ†Œμ™€ ν¬νŠΈμ— μ ‘μ†ν•΄μ„œ html/css/js νŒŒμΌμ„ 전달 λ°›μ•„ 화면에 ν‘œμ‹œν•©λ‹ˆλ‹€. μ΄λŠ” 마치 http://www.google.com 을 μž…λ ₯ ν•œ 것과 λ™μΌν•˜κ²Œ λ™μž‘ν•˜λŠ” κ²ƒμœΌλ‘œ 정상적인 μ›Ήμ„œλ²„κ°€ 이미 κ΅¬λ™λ˜κ³  μžˆμŒμ„ μ˜λ―Έν•©λ‹ˆλ‹€.

λ”°λΌμ„œ μš°λ¦¬κ°€ κ΅¬λ™ν•œ μ„œλ²„μ˜ μ£Όμ†Œ λ’€μ—λŠ”, http://localhost:3000/login κ³Ό 같이 '/'둜 κ΅¬λΆ„ν•˜μ—¬ μš°λ¦¬κ°€ μ›ν•˜λŠ” 경둜λ₯Ό μ„€μ • ν›„ μ–Όλ§ˆλ“ μ§€ μƒˆλ‘œμš΄ νŽ˜μ΄μ§€μ— μ—°κ²° ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Router

ν”„λ‘œμ νŠΈλŠ” 기본적으둜 main.html에 mainμ΄λΌλŠ” μ΄λ¦„μ˜ ν…œν”Œλ¦Ώμ„ κ°€μ§€κ³  있고, main.jsμ—λŠ” λ‹€μŒκ³Ό 같이 '/' μ£Όμ†Œλ‘œ μ ‘κ·Ό μ‹œ main ν…œν”Œλ¦Ώμ„ λΈŒλΌμš°μ €μ— 전달 ν•˜λ„λ‘ λͺ…μ‹œ λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

main.js

FlowRouter.template('/', 'main');

λ§Œμ•½ posts.htmlκ³Ό 같이 μƒˆλ‘œμš΄ ν…œν”Œλ¦Ώμ„ 생성 ν•œ ν›„ λ‹€μŒκ³Ό 같이 μ„€μ • ν•œλ‹€λ©΄ 이제 'http://localhost:3000/posts'을 μ£Όμ†Œμ°½μ— μž…λ ₯ν•˜μ—¬ μ ‘κ·Ό μ‹œ λΈŒλΌμš°μ € 화면에 posts ν…œν”Œλ¦Ώμ„ 그렀주게 될 κ²ƒμž…λ‹ˆλ‹€.

posts.html

<template name="posts">
  <!-- Posts ν™”λ©΄ ꡬ성  -->
</template>

posts.js

FlowRouter.template('/posts', 'posts');

μ£Όμ†Œμ°½μœΌλ‘œ νŒŒλΌλ―Έν„° 전달 λ°›κΈ°

κ²Œμ‹œνŒ λͺ©λ‘μ—μ„œ νŠΉμ • κΈ€μ˜ 제λͺ©μ„ ν΄λ¦­ν•˜λ©΄ μ „ν†΅μ μœΌλ‘œ ν•΄λ‹Ή κ²Œμ‹œκΈ€μ˜ 상세 ν™”λ©΄μœΌλ‘œ λ„˜μ–΄κ°€μ„œ μžμ„Έν•œ λ‚΄μš©λ“€μ„ ν‘œμ‹œ ν•©λ‹ˆλ‹€. μ΄λŸ΄λ•Œμ—λŠ” μ‚¬μš©μžκ°€ κ²Œμ‹œνŒ λͺ©λ‘μ—μ„œ μ–΄λ– ν•œ 글을 μ„ νƒν–ˆλŠ”μ§€ λ‹€μŒ 상세 화면에 μ•Œλ €μ£Όμ–΄μ•Ό ν•  ν•„μš”κ°€ μžˆμŠ΅λ‹ˆλ‹€.

posts.html

<template name="posts">
  <!-- Posts κ²Œμ‹œνŒ λͺ©λ‘ ν™”λ©΄ ꡬ성  -->
  {{#each posts}}
    <a href="/post/{{_id}}">{{title}}</a>
  {{/each}}
</template>

κ²Œμ‹œνŒ λͺ©λ‘ 화면을 μœ„μ™€ 같이 κ΅¬μ„±ν•œ posts.html ν…œν”Œλ¦Ώμ΄ μžˆλ‹€κ³  κ°€μ • ν–ˆμ„ λ•Œ, νƒœκ·Έκ°€ μ˜λ―Έν•˜λŠ” λ°”λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • ν•΄λ‹Ή 라인이 클릭 λ˜μ—ˆμ„λ•Œ /post 경둜의 ν…œν”Œλ¦ΏμœΌλ‘œ μ΄λ™ν•˜λ˜,
  • μ΄λ•Œ /post의 λ’· 뢀뢄에 ν•΄λ‹Ή 글에 포함 된 _id 값을 λΆ™μ—¬μ„œ /post/{{_id}}와 같은 μ£Όμ†Œλ‘œ 접근해라

λ”°λΌμ„œ ν•΄λ‹Ή 링크λ₯Ό μ‚¬μš©μžκ°€ ν΄λ¦­ν•œλ‹€λ©΄ 전체 μ ‘κ·Ό μ£Όμ†ŒλŠ” /post/ κΉŒμ§€λŠ” λ™μΌν•˜λ˜ λ’€μ—λŠ” ν•΄λ‹Ή 글에 포함 된 _id에 ν•΄λ‹Ήν•˜λŠ” UUID 랜덀 값이 포함 되게 될 κ²ƒμž…λ‹ˆλ‹€.

post.js

FlowRouter.template('/post/:_id', 'posts');

Template.post.helpers({
  post: function() {
    var param_id = FlowRouter.getParam('_id');
    return Posts.findOne({_id: param_id});
  }
});

μ΄λ ‡κ²Œ posts ν…œν”Œλ¦Ώμ˜ 링크가 μ‚¬μš©μžμ— μ˜ν•΄ 선택 되면 http://localhost:3000/post/gdkJir6KwfxxP2EGt 와 같이 _idλ₯Ό ν¬ν•¨ν•œ 경둜둜 λΈŒλΌμš°μ €κ°€ μ΄λ™ν•˜κ²Œ 되기 λ•Œλ¬Έμ— post ν…œν”Œλ¦Ώμ˜ .js νŒŒμΌμ—μ„œλŠ” μ΄λŸ¬ν•œ _id값을 핸듀링 ν•  수 μžˆλ„λ‘ μœ„μ™€ 같이 Router μ„€μ • 및 이λ₯Ό μ΄μš”ν•œ 데이터λ₯Ό νŠΉμ •μ§€μ–΄ post ν…œν”Œλ¦Ώμ— 전달해야 ν•©λ‹ˆλ‹€.

post.html

<template name="post">
  <div>{{post.title}}</div>
  <div>{{post.writer}}</div>
  ...
</template>

post.html ν…œν”Œλ¦Ώμ—μ„œλŠ” μ΄λ ‡κ²Œ 전달 받은 post 데이터λ₯Ό μ›ν•˜λŠ” λŒ€λ‘œ μ‚¬μš© ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이제 μ‹±κΈ€ νŽ˜μ΄μ§€ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ 뿐만 μ•„λ‹ˆλΌ μ—¬λŸ¬ νŽ˜μ΄μ§€λ₯Ό κ΅¬μ„±ν•˜μ—¬ 보닀 λ³΅μž‘ν•œ μ›Ή μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μž‘μ„± ν•  μ€€λΉ„κ°€ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.