不能在绑定方法中使用变量

Cannot use variable in bind method

我有以下代码

    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
                    });

请帮我解决这个问题,因为这些值来自网络服务。我不能在这里硬编码。

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();
});

我使用 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);
                    });