Add a JavaScript display to the Home page to count down from 140 characters. (Rails Tutorial, 2nd Ed, Chapter 10, Exercise 7)

前端 未结 7 1088
别跟我提以往
别跟我提以往 2021-02-04 08:32

This exercise was a bit tricky. Figured I\'d post my solution to see if anyone did it differently or if there\'s anyone who knows a better way.

I\'m not sure on best pra

7条回答
  •  情歌与酒
    2021-02-04 09:20

    You can simplify it using CoffeeScript:

    /app/assets/javascripts/microposts.js.coffee

    updateCountdown = ->
      remaining = 140 - jQuery("#micropost_content").val().length
      jQuery(".countdown").text remaining + " characters remaining"
    
    jQuery ->
      updateCountdown()
      $("#micropost_content").change updateCountdown
      $("#micropost_content").keyup updateCountdown
    

    And as mentioned by jonyamo, you don't need to touch the application.js as the //= require_tree . already does the trick.

提交回复
热议问题