2011-10-29 39 views
-2

我正在學習咖啡腳本......我有一些默認爲JS彈出框的代碼 - 但我也想定製一些與mailto和其他內容的鏈接,並默認JS只有在沒有主方向IF/THEN咖啡腳本鏈接

例如...如果用戶點擊請求管理員權限,我想一個mailto以不彈出,上面寫着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:[email protected]">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:[email protected]">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 

回答

4

您使用的方式太多縮進,更糟糕的是,您的評論縮進與其對應的代碼不一致。這使得你的代碼很難閱讀,甚至可能會使編譯器跳閘 - 尤其是如果你使用了製表符和空格(不這樣做!)。

而不是當前

if $(this).text() == 'Request Admin Privileges' 
# <a href="mailto:[email protected]">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()) 
3

哇..如果您選擇的咖啡,然後在寫的樣式。

使用其縮進樣式和一些shugar,如'is','not'等。不要使用額外的語法。

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

嘗試使用一切都是表達式,並返回一些東西的方法。這對if .. else,switch ..語句很有用。

對我來說,使用樣式像

$ "div" 

是可怕的:),需要一些努力copypaste你的示例代碼jQuery的論壇,如果你有一些問題。 jQuery的

其他不良的方法是使用

$(@) 

代替

$(this) 

咖啡是偉大的!正確使用它!