2012-09-04 26 views
1

我從這個博客帖子分叉一個的jsfiddle中創建新的JavaScript對象(只是在星期二晚上的樂趣...) http://boundary.com/blog/2012/07/03/building-node-diagrams-in-html5/從一個版本的本身

而且一直在試圖創建一個鏈接new Node()每次我雙擊一個。我的小提琴是這裏 http://jsfiddle.net/joevallender/cGzCe/4/

我是在第一次試圖與庫不亂,但小提琴包括(資源),我增加了對DblClick事件支持的修改過的版本。

無論如何,要點!我傳遞一個函數在事件上運行,並且它在雙擊firstNode時工作,但我需要不斷重新添加事件到每個新創建的節點/引用創建函數以某種方式猜測我們進入一個循環。

var create = function() { 
    var node = new Node({ 
    title: 'Node', 
    stage: stage, 
    w: NODE_DIMENSIONS.w, 
    h: NODE_DIMENSIONS.h, 
    x: 100, 
    y: 100 
    }).attach(); 
    new Segment({ 
    h: 5, 
    stage: stage, 
    origin: this, 
    destination: node 
    }).attach(); 
} 
var firstNode = new Node({ 
    title: 'Node', 
    stage: stage, 
    w: NODE_DIMENSIONS.w, 
    h: NODE_DIMENSIONS.h, 
    x: 350, 
    y: 50, 
    events: { 
     dblclick: create 
    } 
}).attach(); 

我覺得我應該想要修改雙擊事件的原型函數,但只是有點失落。有沒有人a)知道我在說什麼?和b)你能幫忙嗎?這不是客戶工作或任何事情,但我只想知道!

我已經進行了快速掃描以進行重複掃描,但正如您可能從我措辭不佳的問題標題中看出的那樣,我並不完全知道如何爲我的搜索詞組!

回答

1

完成此操作的最快方法是將dblclick事件添加到創建的節點。你應該可以擴展Node類來自動完成,但我不確定這是否有必要。

var create = function() { 

    var node = new Node({ 
    title: 'Node', 
    stage: stage, 
    w: NODE_DIMENSIONS.w, 
    h: NODE_DIMENSIONS.h, 
    x: 100, 
    y: 100, 
    events:{ // add the event to the created node. 
     dblclick: create 
    } 
    }).attach(); 

    new Segment({ 
    h: 5, 
    stage: stage, 
    origin: this, 
    destination: node 
    }).attach(); 

    //nodes.push(node); 
} 

用於定位新創建的節點,我認爲它是相對於該節點它們是從衍生:

var create = function() { 
    var ancestor = $(event.target); // get the element that was double clicked 
    var top = ancestor.position().top; // and its position 
    var left = ancestor.position().left; 

    var node = new Node({ 
    title: 'Node', 
    stage: stage, 
    w: NODE_DIMENSIONS.w, 
    h: NODE_DIMENSIONS.h, 
    x: left + 100,  //new position relative to the parent. 
    y: top + 100, 
    events:{ 
     dblclick: create 
    } 
    }).attach(); 

    new Segment({ 
    h: 5, 
    stage: stage, 
    origin: this, 
    destination: node 
    }).attach(); 

    //nodes.push(node); 
} 
+0

它似乎我一直有點傻; o)謝謝!我不知道爲什麼我沒有先試過。只是假定它會導致一個問題,但它總是有意義的。乾杯! – joevallender

1

你的代碼工作正常,但問題是在新的節點上重疊彼此之間,因爲在創建功能,你有你的X和Y在100創建新的節點。

,所以我用的Math.random產生隨機的x,y位置到新節點

這裏是Demo

你應該隨意根據自己的需要來產生新的節點位置和名稱

+0

我應該提到我會及時得到隨機位。我只是想先制定出「連續」創作的東西。儘管非常感謝! – joevallender

相關問題