Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从其自身版本中创建新的javascript对象_Javascript - Fatal编程技术网

从其自身版本中创建新的javascript对象

从其自身版本中创建新的javascript对象,javascript,Javascript,我从这篇博文中找到了一个JSFIDLE(只是为了在周二晚上玩…) 每次双击一个节点时,我都试图创建一个链接的新节点()。我的小提琴在这儿 起初,我试图不弄乱这个库,但fiddle(在参考资料中)包含了一个经过修改的版本,其中我添加了对dblclick事件的支持 无论如何,说到点子上!我正在传递一个函数,在事件上运行,当双击firstNode时,它确实可以工作,但我需要不断地将事件重新添加到每个新创建的节点中/以某种方式引用create函数,这会将我们猜测到一个循环中 var create =

我从这篇博文中找到了一个JSFIDLE(只是为了在周二晚上玩…)

每次双击一个节点时,我都试图创建一个链接的
新节点()。我的小提琴在这儿

起初,我试图不弄乱这个库,但fiddle(在参考资料中)包含了一个经过修改的版本,其中我添加了对dblclick事件的支持

无论如何,说到点子上!我正在传递一个函数,在事件上运行,当双击
firstNode
时,它确实可以工作,但我需要不断地将事件重新添加到每个新创建的节点中/以某种方式引用create函数,这会将我们猜测到一个循环中

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();
我觉得我应该为双击事件修改原型函数,但只是有点迷失了方向。有人知道我在说什么吗?b)你能帮忙吗。这不是客户的工作或任何事情,但我只是想知道


我已经快速扫描了重复的内容,但正如你可能从我措辞糟糕的问题标题中看到的那样,我不完全确定如何表达我的搜索

实现这一点的最快方法是向创建的节点添加
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);
}

您的代码工作正常,但问题是新节点彼此重叠,因为在create函数中,您的x和y为100以创建新节点

所以我使用math.random生成随机的x,y位置到新节点

这是你的电话号码


您应该根据需要随机生成新的节点位置和名称

我应该提到,我会及时获得随机位。我只是想先解决“连续”创作的问题。非常感谢!看来我有点傻;o) 谢谢!我不知道为什么我没有先尝试。我只是认为这会引起问题,但这完全有道理。干杯