nerfdoit
nerfdoit

Reputation: 41

ReferenceError: g is not defined

I am new to JointJS and I am trying to follow this tutorial about creating links when dropping elements on top of each other.

When I try to run it I get this error in the chrome console:

Uncaught ReferenceError: g is not defined

paper.on('cell:pointerup', function(cellView, evt, x, y) {
    // Find the first element below that is not a link nor the dragged element itself.
    var elementBelow = graph.get('cells').find(function(cell) {
        if (cell instanceof joint.dia.Link) return false; // Not interested in links.
        if (cell.id === cellView.model.id) return false; // The same element as the dropped one.
        if (cell.getBBox().containsPoint(g.point(x, y))) {
            return true;
        }
        return false;
    });

    // If the two elements are connected already, don't
    // connect them again (this is application specific though).
    if (elementBelow && !_.contains(graph.getNeighbors(elementBelow), cellView.model)) {

        graph.addCell(new joint.dia.Link({
            source: { id: cellView.model.id }, target: { id: elementBelow.id },
            attrs: { '.marker-source': { d: 'M 10 0 L 0 5 L 10 10 z' } }
        }));
        // Move the element a bit to the side.
        cellView.model.translate(200, 0);
    }
});

I see that g is never created in the code above and it isn't created in the tutorial either, what is it referencing to?

Upvotes: 3

Views: 5273

Answers (1)

CPHPython
CPHPython

Reputation: 13719

g (Geometry documentation) allows to invoke several different functions to return specific objects or to operate on points, lines and shapes. Example:

g.point(3,4) // -> {x: 3, y: 4}

If g is not defined, it means one of the JS files you are using is either missing it's g declaration at the beginning (var g = window.g || {};) or it is trying to use it before it is declared by JointJS/Rappid.

Upvotes: 2

Related Questions