Search code examples
javascriptjqueryjsplumb

how to pass the variable from the nested function call


I am doing a nested function call but with the same time I need to pass the variable to the nested function can i do that. Here is what I am trying to do so

allSourceEndpoints.push(jsPlumb.addEndpoint(toId, sourceEndpoint(index), { anchor:sourceAnchors[i], uuid:sourceUUID }));


sourceEndpoint(index) = {
            endpoint:"Dot",

            paintStyle:{ fillStyle:"#225588",radius:3 },
            isSource:true,
            isTarget:true,
            maxConnections:-1,
        //  connector:[ "Flowchart", { stub:[40, 60], gap:10 } ],
        //  connector:[ "Flowchart"],

            hoverPaintStyle:connectorHoverStyle,
            connectorHoverStyle:connectorHoverStyle,
            dragOptions:{},
            overlays:[
                [ "Label", { 
                    location:[0.5, 1.5], 
                    label:""+startEnd[index].start,
                    cssClass:"endpointSourceLabel",
                } ]
            ]
        }

The above code does not work because of the

       index 

passing that i am doing. I need that as i need to find out the start. If I remove that index reference and the line

         label:""+startEnd[index].start,

it works fine but I really need to include that. Is there a way to do this??

Thanks a lot for the help!


Solution

  • Change the sourceEndPoint construct to a function and return the JSON object as a return value. i.e.:

    sourceEndpoint = function(index) {
    
        return {
                endpoint:"Dot",
    
                paintStyle:{ fillStyle:"#225588",radius:3 },
                isSource:true,
                isTarget:true,
                maxConnections:-1,
            //  connector:[ "Flowchart", { stub:[40, 60], gap:10 } ],
            //  connector:[ "Flowchart"],
    
                hoverPaintStyle:connectorHoverStyle,
                connectorHoverStyle:connectorHoverStyle,
                dragOptions:{},
                overlays:[
                    [ "Label", { 
                        location:[0.5, 1.5], 
                        label:""+startEnd[index].start,
                        cssClass:"endpointSourceLabel",
                    } ]
                ]
            };
        }