简体   繁体   English

在play框架中使用coffeescript的正确方法是什么?

[英]What is the proper way of using coffeescript in play framework?

I am a new learner of play framework. 我是游戏框架的新手。 While i am following Play Framework Essentials books, I tried coffeescript example which will delete added items but i failed the button doesn't response. 当我关注Play Framework Essentials书籍时,我尝试了coffeescript示例,该示例将删除添加的项目,但我失败了,按钮没有响应。

shop.coffee shop.coffee

 require(['logic'], (Logic) ->
  for deleteBtn in  document.querySelectorAll('button.delete-item')
    do  (deleteBtn) ->  Logic(deleteBtn,deleteBtn.dataset.id)
)

logic.coffee logic.coffee

define(['ui', 'routes'],    (Ui,routes) ->
  (node,id) ->
    ui  =   Ui(node)
    ui.forEachClick(()  ->
      xhr   =new XMLHttpRequest()
      route = routes.controllers.Items.delete(id)
      xhr.open(route.method,    route.url)
      xhr.addEventListener('readystatechange',  ()  ->
        if  xhr.readyState  ==  XMLHttpRequest.DONE
          if    xhr.status  ==  200
            ui.delete()
          else
            alert('Unable   to  delete  the item!')
      )
      xhr.send()
    )
)

ui.coffee ui.coffee

define(() ->
  (node) ->
    delete: ()  ->
      li = node.parentNode
      li.parentNode.removeChild(li)
    forEachClick: (callback)    ->
      node.addEventListener('click', callback)

)

index.scala.html index.scala.html

<script type="text/javascript" src="@routes.Application.javascriptRoutes"></script>
<script src="@routes.Assets.at("javascripts/shop.js")"></script>

Also javascript reverse routing is used to avoid duplication. 此外,javascript反向路由还可用于避免重复。

def javascriptRoutes    =   Action  {   implicit    request =>
 val javascriptR = JavaScriptReverseRouter("routes")(
          routes.javascript.Items.delete
        )
    //I didnt figure out, something is wrong with coffeescript file...
        Ok(JavaScript(
          s"""
             define(function()  { $javascriptR; return  routes  })
           """
        ))
      }

Note: I have done this with javascript example. 注意:我已经使用javascript示例完成了此操作。 What i realize is coffeescript easier to learn that's why i want to figure out the problem. 我意识到,coffeescript更容易学习,这就是为什么我想找出问题所在。

我认为您可以从播放文档https://www.playframework.com/documentation/2.5.x/AssetsCoffeeScript中获得一些想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM