๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐ŸŽž๏ธ JavaScript

Ajax/JQuery | ์ข‹์•„์š” ๊ธฐ๋Šฅ

  • -

์„œ๋ฒ„

  • ์šฐ์„  ์„œ๋ฒ„ ์ชฝ ๊ธฐ๋Šฅ์„ ๋จผ์ € ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค. ํ•˜ํŠธ๋ฅผ ๋ˆ„๋ฅด๋ฉด 1) ์–ด๋–ค ํฌ์ŠคํŠธ๋ฅผ 2) ๋ˆ„๊ฐ€ ๋ˆŒ๋ €๊ณ  3) ์ข‹์•„์š”์ธ์ง€ ์ข‹์•„์š” ์ทจ์†Œ์ธ์ง€๋ฅผ ์•Œ์•„์•ผ๊ฒ ์ฃ ? ์ˆ™์ œ๋กœ ๋งŒ๋“ค ๋‹ค๋ฅธ ๋ฐ˜์‘๋“ค(โญ, ๐Ÿ‘)์„ ์ƒ๊ฐํ•˜๋ฉด ์–ด๋Š ์•„์ด์ฝ˜์„ ๋ˆŒ๋ €๋Š”์ง€๋„ ์•Œ์•„์•ผ๊ฒ ๋„ค์š”.
  • DB์— ์ €์žฅํ•  ๋•Œ๋Š” 1) ๋ˆ„๊ฐ€ 2) ์–ด๋–ค ํฌ์ŠคํŠธ์— 3) ์–ด๋–ค ๋ฐ˜์‘์„ ๋‚จ๊ฒผ๋Š”์ง€ ์„ธ ์ •๋ณด๋งŒ ๋„ฃ์œผ๋ฉด ๋˜๊ณ , ์ข‹์•„์š”์ธ์ง€, ์ทจ์†Œ์ธ์ง€์— ๋”ฐ๋ผ ํ•ด๋‹น ๋„ํ๋จผํŠธ๋ฅผ insert_one()์„ ํ• ์ง€ delete_one()์„ ํ• ์ง€ ๊ฒฐ์ •ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
if action_receive =="like":
    db.likes.insert_one(doc)
else:
    db.likes.delete_one(doc)
  • ์ข‹์•„์š” ์ปฌ๋ ‰์…˜์„ ์—…๋ฐ์ดํŠธํ•œ ์ดํ›„์—๋Š” ํ•ด๋‹น ํฌ์ŠคํŠธ์— ํ•ด๋‹น ํƒ€์ž…์˜ ๋ฐ˜์‘์ด ๋ช‡ ๊ฐœ์ธ์ง€๋ฅผ ์„ธ์„œ ๋ณด๋‚ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
 count = db.likes.count_documents({"post_id": post_id_receive, "type": type_receive})

 

API

user_info = db.users.find_one({"username": payload["id"]})
post_id_receive = request.form["post_id_give"]
type_receive = request.form["type_give"]
action_receive = request.form["action_give"]
doc = {
    "post_id": post_id_receive,
    "username": user_info["username"],
    "type": type_receive
}
if action_receive =="like":
    db.likes.insert_one(doc)
else:
    db.likes.delete_one(doc)
count = db.likes.count_documents({"post_id": post_id_receive, "type": type_receive})
return jsonify({"result": "success", 'msg': 'updated', "count": count})

 

ํด๋ผ์ด์–ธํŠธ

  • API์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ์šฉ์ž ์ •๋ณด, ํฌ์ŠคํŠธ ์•„์ด๋””, ์ข‹์•„์š”/์ข‹์•„์š” ์ทจ์†Œ, ์•„์ด์ฝ˜ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค.
  • ์—ฌ๊ธฐ์—์„œ ํ•˜ํŠธ๋ฅผ ๋ˆ„๋ฅธ ์‚ฌ๋žŒ์˜ ์ •๋ณด๋Š” ๋กœ๊ทธ์ธ ์ •๋ณด์—์„œ ๋ฐ›์•„์™”์œผ๋ฏ€๋กœ ๋‚˜๋จธ์ง€ 3๊ฐœ๋งŒ ๋ฐ์ดํ„ฐ๋กœ ๋ณด๋‚ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ์ข‹์•„์š”์ธ์ง€, ์ข‹์•„์š” ์ทจ์†Œ์ธ์ง€๋Š” ์•„์ด์ฝ˜์˜ ํด๋ž˜์Šค๊ฐ€ fa-heart์ธ์ง€ fa-heart-o์ธ์ง€๋กœ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์—…๋ฐ์ดํŠธ์— ์„ฑ๊ณตํ•˜๋ฉด ์•„์ด์ฝ˜์˜ ํด๋ž˜์Šค๋ฅผ ๋ฐ”๊พธ๊ณ  ์ข‹์•„์š” ์ˆซ์ž๋„ ์—…๋ฐ์ดํŠธํ•ด์ค๋‹ˆ๋‹ค.
function toggle_like(post_id, type) {
    console.log(post_id, type)
    let $a_like = $(`#${post_id} a[aria-label='heart']`)
    let $i_like = $a_like.find("i")
    if ($i_like.hasClass("fa-heart")) {
        $.ajax({
            type: "POST",
            url: "/update_like",
            data: {
                post_id_give: post_id,
                type_give: type,
                action_give: "unlike"
            },
            success: function (response) {
                console.log("unlike")
                $i_like.addClass("fa-heart-o").removeClass("fa-heart")
                $a_like.find("span.like-num").text(response["count"])
            }
        })
    } else {
        $.ajax({
            type: "POST",
            url: "/update_like",
            data: {
                post_id_give: post_id,
                type_give: type,
                action_give: "like"
            },
            success: function (response) {
                console.log("like")
                $i_like.addClass("fa-heart").removeClass("fa-heart-o")
                $a_like.find("span.like-num").text(response["count"])
            }
        })

    }
}

 

์ข‹์•„์š” ์ˆซ์ž ํ‘œ์‹œํ•˜๊ธฐ

  • ์šฐ์„  ์„œ๋ฒ„์—์„œ ํฌ์ŠคํŠธ ๋ชฉ๋ก์„ ๋ณด๋‚ด์ค„ ๋•Œ ๊ทธ ํฌ์ŠคํŠธ์— ๋‹ฌ๋ฆฐ ํ•˜ํŠธ๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€, ๋‚ด๊ฐ€ ๋‹จ ํ•˜ํŠธ๋„ ์žˆ๋Š”์ง€ ๊ฐ™์ด ์„ธ์–ด ๋ณด๋‚ด์ค๋‹ˆ๋‹ค.
for post in posts:
    post["_id"] = str(post["_id"])
    post["count_heart"] = db.likes.count_documents({"post_id": post["_id"], "type": "heart"})
    post["heart_by_me"] = bool(db.likes.find_one({"post_id": post["_id"], "type": "heart", "username": payload['id']}))
  • ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” ์ด ์ •๋ณด๋ฅผ ๋ฐ›์•„ ์ฐฌ ํ•˜ํŠธ("fa-heart")๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ธ์ง€, ๋นˆ ํ•˜ํŠธ("fa-heart-o")๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
let class_heart = ""
if (post["heart_by_me"]) {
    class_heart = "fa-heart"
} else {
    class_heart = "fa-heart-o"
}
  • ์ด๊ฒƒ์„ '์กฐ๊ฑด๋ถ€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž(ternary operator)'๋ฅผ ์“ฐ๋ฉด ํ•œ ์ค„๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์–ด์š”!
let class_heart = post['heart_by_me'] ? "fa-heart": "fa-heart-o"
  • ์ด ์ •๋ณด๋ฅผ html_temp๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•˜ํŠธ ๊ฐœ์ˆ˜์™€ ํ•จ๊ป˜ ๋„ฃ์–ด์ฃผ๋ฉด ๋!
<span class="icon is-small"><i class="fa ${class_heart}" aria-hidden="true"></i></span>
&nbsp;
<span class="like-num">${post["count_heart"]}</span>

 

์ข‹์•„์š” ์ˆซ์ž๋„ ํ˜•์‹์„ ์กฐ๊ธˆ ๋ฐ”๊ฟ”๋ณผ๊นŒ์š”?

  • ์šฐ์„  10,000๊ฐœ๊ฐ€ ๋„˜์œผ๋ฉด '12k'์ฒ˜๋Ÿผ ์ •์ˆ˜+k ํ˜•์‹์œผ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
  • 500๊ฐœ๊ฐ€ ๋„˜์œผ๋ฉด '0.5k'์ฒ˜๋Ÿผ ์†Œ์ˆซ์  ์•„๋ž˜ ํ•œ ์ž๋ฆฌ ์ˆ˜์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•ด์ค๋‹ˆ๋‹ค.
  • ์ข‹์•„์š” ์ˆ˜๊ฐ€ 0๊ฐœ์ผ ๋•Œ๋Š” ์ˆซ์ž๋ฅผ ์ ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์ž‘์€ ์ˆซ์ž๋Š” ๊ทธ๋Œ€๋กœ ์ ์Šต๋‹ˆ๋‹ค.
function num2str(count) {
    if (count > 10000) {
        return parseInt(count / 1000) + "k"
    }
    if (count > 500) {
        return parseInt(count / 100) / 10 + "k"
    }
    if (count == 0) {
        return ""
    }
    return count
}
  • get_posts()์™€ toggle_like() ์•ˆ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
// get_posts()
<span class="icon is-small"><i class="fa ${class_heart}" aria-hidden="true"></i></span>
&nbsp;
<span class="like-num">${num2str(post["count_heart"])}</span>

// toggle_like()
$a_like.find("span.like-num").text(num2str(response["count"]))

 

JS ํŒŒ์ผ ๋ถ„๋ฆฌํ•˜๊ธฐ

  • ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€์— ๊ฑธ์ณ ์“ฐ์ด๋Š” CSS ๋‚ด์šฉ์„ mystyle.css ํŒŒ์ผ์— ์ ์–ด ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋“ฏ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋„ ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋‹ค๋ฅธ html์—์„œ ์ž„ํฌํŠธํ•ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํฌ์ŠคํŒ… ๊ด€๋ จ ํ•จ์ˆ˜๋“ค์€ ํ”„๋กœํ•„ ํŽ˜์ด์ง€์—์„œ๋„ ์“ฐ์ด๋ฏ€๋กœ JS ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ๋ด…์‹œ๋‹ค.
  • static ํด๋” ์•ˆ์— myjs.js ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ํ•จ์ˆ˜๋“ค์„ ๋ณต์‚ฌํ•ด ๋„ฃ์Šต๋‹ˆ๋‹ค.
  • HTML ์•ˆ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ž„ํฌํŠธํ•ด์˜ต๋‹ˆ๋‹ค.
<script src="{{ url_for('static', filename='myjs.js') }}"></script>
 
 
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.