Nodejs : Wordbucket GitHub Link
Commit 11 Nodejs Wordbucket : can add word with explanation
Commits on May 1, 2018
- เนื่องจากใน model node version ที่ใช้ไม่สามารถใช้ association จาก model ได้ ต้องสร้าง association จากใน index.js "const Explanation_Word = Explanation.belongsTo(Word, {as: 'Explanation_Word'});"
- สร้างตัวแปร message ได้เก็บ message ที่จะเตือน user เมื่อพยายาม add duplicate / ช่องว่าง ลงใน database
- update function addingNewWord() ให้ add explanation พร้อม word ได้เหมือนใน django โดยถ้า add duplicate / ช่องว่าง จะไม่ add ลงใน database
- เพิ่ม function renderDisplayWithMessage ไว้ render index.ejs พร้อม message (เพราะ ถ้าใส่ในหน้าเดิมเมื่อ refresh message จะยังอยู่ ไม่หายไปเหมือน django) จึงเขียนกันไว้
- เพิ่ม function view_word() เพื่อดู explanation ของ word นั้นๆ
114 index.js
migrations/20180507184836-create-explanation.js
- เมื่อทำการ สร้าง association จากใน index.js "const Explanation_Word = Explanation.belongsTo(Word, {as: 'Explanation_Word'});" จะ auto สร้างตัวแปร foreign key ของความสัมพันธ์ one to many ในไฟล์ migration ของตาราง explanation จึงลบ entity word ทิ้ง ก่อน migration ใหม่
models/explanation.js
- เพื่อใช้ ExplanationWordId จึงได้ทำการใส่ไว้ใน model และกำหนดความสัมพันธ์ไว้ข้างล่าง(ไม่อย่างนั้นจะ migrate ไม่ผ่านแต่ก็ยังต้องกำหนดความสัมพันธ์ไว้ใน index.js อยู่ดีถึงใช้ได้)
| @@ -1,14 +1,14 @@ | ||
| 'use strict'; | ||
| module.exports = (sequelize, DataTypes) => { | ||
| var Explanation = sequelize.define('Explanation', { | ||
| - word: DataTypes.STRING, | ||
| + ExplanationWordId: DataTypes.STRING, | ||
| explanation_text: DataTypes.STRING, | ||
| like: DataTypes.INTEGER, | ||
| dislike: DataTypes.INTEGER | ||
| }, {}); | ||
| Explanation.associate = function(models) { | ||
| // associations can be defined here | ||
| - Explanation.belongsTo(models.Word); | ||
| + Explanation.belongsTo(models.Word, { foreignKey: 'word' }); | ||
| }; | ||
| return Explanation; | ||
| }; |
views/detail.ejs
- สร้าง template detail ขึ้นเพื่อดู explanations ของ word นั้นๆโดยนำมาจาก project django มาดัดแปลงใช้ในรูปแบบของ ejs
| @@ -0,0 +1,31 @@ | ||
| +<html> | ||
| + | ||
| + <head> | ||
| + <title> WordBucket App </title> | ||
| + <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet"> | ||
| + <link href="/styles.css" rel="stylesheet"> | ||
| + </head> | ||
| + | ||
| +<body> | ||
| + <div class="container"> | ||
| + | ||
| + <h1>WordBucket App</h1> | ||
| + | ||
| + <form action ="/addword" id="form1" method="POST"> | ||
| + <input name="explanation_input" id="id_new_eplanation" placeholder="Add explanation" /> | ||
| + </form> | ||
| + <button type="submit" form="form1" value="Submit">Submit</button> | ||
| + | ||
| + <h2><%= word %></h2> | ||
| + | ||
| + <table id="id_word_table"> | ||
| + <% for( var i = 0; i < explanation.length; i++){ %> | ||
| + <tr><td id="word.word">explanation <%= i+1 + " : " + explanation[i] %></td></tr> | ||
| + <% } %> | ||
| + </table> | ||
| + <br><br><br> | ||
| + <a href="/">home</a> | ||
| +</div> | ||
| + | ||
| +</body> | ||
| +</html> |
views/index.ejs
- ลบการแสดงผลแบบเก่า ที่เพื่อนทำเหมือนใน To-Do app (link) เป็นในรูปแบบที่ทำใน project django แต่เปลี่ยนเป็นใช้รูปแบบของ ejs
ไม่มีความคิดเห็น:
แสดงความคิดเห็น