无法在绑定方法中使用变量

时间:2019-03-05 14:37:06

标签: javascript string object bind ready

我有以下代码

    jsPlumb.bind("ready", function() {          
                    jsPlumb.importDefaults({
                        Anchors  : ["RightMiddle", "LeftMiddle"],
                        EndpointStyles : [{ fillStyle:'#55636b' }, { fillStyle:'#55636b' }],
                        Endpoints : [ [ "Rectangle", {width:1, height:1} ], [ "Rectangle", { width:1, height:1 } ]],
                        ConnectionOverlays  : [
                            [ "Arrow", { location: -2 , width: 15, length: 15 } ]
                        ],
                        Connector:[ "Flowchart", { stub: 10, gap:10 } ],                                
                        PaintStyle  : {
                            lineWidth:2,
                            strokeStyle:"#55636b",
                            joinstyle:"round"
                        }
                    });

                //XSSOK
                    jsPlumb.connect({ source:'start', target:'task0' });
                    jsPlumb.connect({ source:'task0', target:'end' });
                });

在上面的代码的最后两行中,如果我直接在bind方法中使用它就可以了。

jsPlumb.connect({ source:'start', target:'task0' });
jsPlumb.connect({ source:'task0', target:'end' });

但是,如果我将相同的值存储在变量中并使用该变量,则它将停止工作。

jsPlumb.bind("ready", function() {          
                        jsPlumb.importDefaults({
                            Anchors  : ["RightMiddle", "LeftMiddle"],
                            EndpointStyles : [{ fillStyle:'#55636b' }, { fillStyle:'#55636b' }],
                            Endpoints : [ [ "Rectangle", {width:1, height:1} ], [ "Rectangle", { width:1, height:1 } ]],
                            ConnectionOverlays  : [
                                [ "Arrow", { location: -2 , width: 15, length: 15 } ]
                            ],
                            Connector:[ "Flowchart", { stub: 10, gap:10 } ],                                
                            PaintStyle  : {
                                lineWidth:2,
                                strokeStyle:"#55636b",
                                joinstyle:"round"
                            }
                        });                 
                    sbConnections
                    });

请帮助我解决此问题,因为这些值来自Web服务。我不能在这里对其进行硬编码。

2 个答案:

答案 0 :(得分:0)

Functions are first class objects in JavaScript。为了将连接与绑定分开。我将执行以下操作:

var sbConnections = function () {
    jsPlumb.connect({ source:'start', target:'task0' });
    jsPlumb.connect({ source:'task0', target:'end' });
};

// Later on
jsPlumb.bind("ready", function() {
  jsPlumb.importDefaults({
    Anchors: ["RightMiddle", "LeftMiddle"],
    EndpointStyles: [{ fillStyle: '#55636b' }, { fillStyle: '#55636b' }],
    Endpoints : [ [ "Rectangle", {width:1, height:1} ], [ "Rectangle", { width:1, height:1 } ]],
    ConnectionOverlays: [[ "Arrow", { location: -2 , width: 15, length: 15 } ]],
    Connector:[ "Flowchart", { stub: 10, gap:10 } ],                                
    PaintStyle: {
        lineWidth: 2,
        strokeStyle: "#55636b",
        joinstyle: "round"
    }
  });

  // invoke the function
  sbConnections();
});

答案 1 :(得分:0)

我通过使用eval函数解决了这个问题

jsPlumb.bind("ready", function() {          
                        jsPlumb.importDefaults({
                            Anchors  : ["RightMiddle", "LeftMiddle"],
                            EndpointStyles : [{ fillStyle:'#55636b' }, { fillStyle:'#55636b' }],
                            Endpoints : [ [ "Rectangle", {width:1, height:1} ], [ "Rectangle", { width:1, height:1 } ]],
                            ConnectionOverlays  : [
                                [ "Arrow", { location: -2 , width: 15, length: 15 } ]
                            ],
                            Connector:[ "Flowchart", { stub: 10, gap:10 } ],                                
                            PaintStyle  : {
                                lineWidth:2,
                                strokeStyle:"#55636b",
                                joinstyle:"round"
                            }
                        });                 
                    eval(sbConnections);
                    });