2012-02-08 30 views
0

我打電話給這個函數並在這個地方得到錯誤:data: { key: node.parent.data.key } saing「Unexpected {」。有什麼不對。因爲我找不到錯誤。Coffescript沒有編譯有效的源代碼

$("#discipline-list", @el).dynatree({ 
     fx: { height: "toggle", 
     duration: 100 }, 
     initAjax: { 
      url: "/disciplines", 
      data: { mode: "funnyMode" } 
     }, 
     onLazyRead: (node) -> 
      console.log(node); 
      node.appendAjax({url: "/disciplines_details", 
       data: { key: node.parent.data.key } 
      }); 
     }); 

回答

0

我不知道到底什麼是錯的,但更規範的方式來寫這將是

node.appendAjax 
    url: "/disciplines_details" 
    data: 
    key: node.parent.data.key 

有了這樣的編譯錯誤,總是先去Try Coffeescript,看看它是如何得到解析。這使得在大多數情況下修復非常簡單快捷。

2

咖啡腳本不讚賞在同一行上有匿名對象屬性。添加一個換行符修復了這個...

$("#discipline-list", @el).dynatree({ 
     fx: { height: "toggle", 
     duration: 100 }, 
     initAjax: { 
      url: "/disciplines", 
      data: { mode: "funnyMode" } 
     }, 
     onLazyRead: (node) -> 
      console.log(node); 
      node.appendAjax({ 
       url: "/disciplines_details", 
       data: { key: node.parent.data.key } 
      }); 
     }); 

編輯:如何JS轉換成咖啡腳本...

http://js2coffee.org/並粘貼JS(從您的版本修正)

$("#discipline-list", this.el).dynatree({ 
    fx: { height: "toggle", 
    duration: 100 }, 
    initAjax: { 
     url: "/disciplines", 
     data: { mode: "funnyMode" } 
    }, 
    onLazyRead: function(node){ 
     console.log(node); 
     node.appendAjax({ url: "/disciplines_details", 
      data: { key: node.parent.data.key } 
     }); 
    } 
}); 

你將最終形成良好的咖啡腳本...

$("#discipline-list", @el).dynatree 
    fx: 
    height: "toggle" 
    duration: 100 

    initAjax: 
    url: "/disciplines" 
    data: 
     mode: "funnyMode" 

    onLazyRead: (node) -> 
    console.log node 
    node.appendAjax 
     url: "/disciplines_details" 
     data: 
     key: node.parent.data.key 
+1

此外,正如@jkbr所示,它可以刪除很多語法以使其在咖啡腳本中更具可讀性 – 2012-02-08 00:34:36

0

在同一條線上的對象屬性混淆解析器:

node.appendAjax({url: "/disciplines_details", 

只是移動url到下一行,它應該工作:

node.appendAjax({ 
    url: "/disciplines_details", 

這就是說,你還在編寫JavaScript 。

空格在咖啡標記中很重要(即不能縮小它)。正確的縮進是必不可少的,而且這個代碼都是錯誤的。修復壓痕,擺脫逗號和分號:

$("#discipline-list", @el).dynatree({ 
    fx: { 
     height: "toggle" 
     duration: 100 
    } 
    initAjax: { 
     url: "/disciplines", 
     data: { mode: "funnyMode" } 
    } 
    onLazyRead: (node) -> 
     console.log(node) 
     node.appendAjax({ 
      url: "/disciplines_details" 
      data: { key: node.parent.data.key } 
     }) 
}) 

然後繼續擺脫括號,括號中爲比利的最後樣本中的@。如果你不舒服,你應該嘗試堅持使用普通的JavaScript一段時間。