2015-01-05 42 views
1

我想傳遞一個附加參數到graffle的連接函數,這樣我就可以根據該新參數在該行的末尾繪製一個帶/不帶箭頭的連接。如何傳遞一個額外的參數graffle.js

要繪製一個方面,我常做這樣的事情:

connections.push(paper.connection(obj1, obj2, "#000")); 

現在我想這種方式添加新參數:

connections.push(paper.connection(condCol, ret, "#000",true)); 

但參數始終是undefined

這是我改變連接功能的方式:

Raphael.fn.connection = function (obj1, obj2, line, bg, addArrow) { 

    //... 

    if (line && line.line) { 
     //... 
    } else { 
     // based on the 'addArrow' parameter I can figure out 
     // whether I should put an arrow at the end of the line or not 
     var settings; 
     if (addArrow) { 
      //If addArrow was true, draw a arrow at the end of the line 
      settings = { "stroke": color, fill: "none", 'stroke-width': 2, 'arrow-end': 'block-midium-midium' }; 
     } 
     else { 
      //If addArrow was false, just draw a line 
      settings = { "stroke": color, fill: "none", 'stroke-width': 2 }; 
     } 
     return { 
      bg: bg && bg.split && this.path(path).attr({ stroke: bg.split("|")[0], fill: "none", "stroke-width": bg.split("|")[1] || 3 }), 
      line: this.path(path).attr(settings), //use the settings 
      from: obj1, 
      to: obj2 
     }; 
    } 

} 

你有什麼想法,爲什麼這是行不通的?

在此先感謝。

回答

2

您的新connection()函數有五個參數。你只能通過它四個。這就是爲什麼addArray未定義。您傳遞的true值正在進入bg參數。嘗試這樣調用:

connections.push(paper.connection(condCol, ret, "#000", null, true)); 
+1

爲'bg'參數傳遞'null'或空字符串有什麼不同? – Axel

+0

在這種情況下,由於代碼測試b是否存在的方式並不重要。 JS中的空字符串在轉換爲布爾值時被視爲false。所以是空的。但其他代碼可能會以另一種方式進行測試。 –

1

對不起,我沒有注意到這些是可選參數。 解決方案非常簡單。 我只是改變了方法的定義是這樣的:

Raphael.fn.connection = function (obj1, obj2, line, bg, addArrow) { 

} 

,並調用功能,而忽略了第四個參數:

connections.push(paper.connection(condCol, ret, "#000","",true)); 

就是這樣。