IF / THEN咖啡脚本链接

IF / THEN coffee script links

本文关键字:脚本 链接 咖啡 THEN IF      更新时间:2023-09-26

我正在学习咖啡脚本…我有一些代码,默认为一个JS弹出-但我也想自定义一些邮件和其他内容的链接,并有JS默认只有当没有主方向

例如

…如果用户点击请求管理权限,我想要一个邮件来弹出,而不是JS说"此功能尚未可用"

onPhotosLoaded = (sampledata) ->
            if !sampledata || sampledata?.success == false
                jvmw.empty().append('Gallery not found')
                return
            # once the photos are loaded
            jvu.waitCss $("#jv_mw"), 'jv-colorbox-css-loaded', ->
                $.colorbox.init()
                jvmw.empty()
                afterPhotosAndTags() # don't load tags before getting here.
                email = "#{gal_code}@phto.us"
                addMorePhotosInfoDiv = $ """<div/>""" 
                addMorePhotosButton = $ """<div style="float:left;font-size:32px"><br>Everyone's photos from "<span class="jvreplacewitheventname">this event</span>" <a class="jvaddmorephotosbutton" href="#">+Add yours!</a> <br/> </div>"""
                addMorePhotosInfoDiv.append addMorePhotosButton
                dragDropDiv = $ '<div class="jvdragzoneparent" />'
                addMorePhotosInfoDiv.append(dragDropDiv).append('<div style="clear:both;" />')
                extraTestButtons = $ '<div id="jvextratestbut" />'
                extraTestButtons.append('<a href="mailto:adminplease@albumpl.us">Request Admin Privileges</a> |  | <a href="#">Turn SMART BROWSE (ON)</a> | ' + 
                    ' | <a href="#">Purchase prints and other merchandise</a>|| <a href="http://www.albumpl.us/gallery/#{gal_code}/live">Live View</a>')
                extraTestButtons.find('a').click ->
            if $(this).text() == 'Request Admin Privileges'
            # <a href="mailto:adminplease@albumpl.us">Request Admin Privileges</a>
                 return false
            # default thing to do is show the dialog - and register the event.
                showNotYetAvailableMessage($(this).text())
     return false
    extraTestButtons.find('a').click ->
  if $(this).text() == 'Live View'
    # <a href="http://www.albumpl.us/gallery/#{gal_code}
            return false
  # default thing to do is show the dialog - and register the event.
          showNotYetAvailableMessage($(this).text())
         return false

您使用了太多的缩进,更糟糕的是,您的注释与它们对应的代码的缩进不一致。这会使您的代码很难阅读,甚至可能会使编译器出错—特别是如果您同时使用制表符和空格(不要这样做!)。

代替当前

if $(this).text() == 'Request Admin Privileges'
# <a href="mailto:adminplease@albumpl.us">Request Admin Privileges</a>
     return false
# default thing to do is show the dialog - and register the event.
    showNotYetAvailableMessage($(this).text())

if $(this).text() == 'Request Admin Privileges'
  return false
showNotYetAvailableMessage($(this).text())

,甚至使用CoffeeScript的后缀条件

return false if $(this).text() == 'Request Admin Privileges'
showNotYetAvailableMessage($(this).text())

哇…如果你选择咖啡,那就写它的风格。

使用它的缩进样式和一些字符,如'is', 'not'等。不要使用额外的语法

if $(this).text() is "Request Admin Privilegies"
  return false 
showNotYetAvailableMessage $(this).text()

尝试使用方法,一切都是表达式,它返回一些东西。它对……是有用的。别的,开关. .语句。

对于我来说,使用像

这样的样式
$ "div"

是可怕的:),需要一些努力复制你的代码,例如到jquery论坛,如果你有一些问题。

另一个不好的jQuery方法是使用

$(@)
不是

$(this)

咖啡好极了!正确使用它!