Shahid
Shahid

Reputation: 105

Why changes are not saved in file in SVG-Edit?

function FileLoad(file) {
    svgEditor.loadFromURL(file);
    var svg = document.getElementById("svgID");
    code continues....
}

I am trying to manipulate a svg after it gets load.

But the code is not further executed,neither it is shown in developer tool

I am using SVG-Edit toool.

Upvotes: 0

Views: 135

Answers (1)

ccprog
ccprog

Reputation: 21821

svgEditor.loadFromURL() returns a Promise. Loading, by its nature, is always an asynchronuous operation. You need to wait until it is done:

function FileLoad(file) {
    svgEditor.loadFromURL(file).then(function () {
        var svg = document.getElementById("svgID");
        // code continues....
    }, function (error) {
        // load error handling
    });
}

From the naming of your function it looks as if you use it for instantiating an object. However you use the FileLoad function, notice that the SVG content will not be available synchronuously, but only after the promise has resolved. In the case of calling it with new, a possible pattern could look like this (the callback function containing everything you do with the instantiated object):

function FileLoad(file, callback) {
    svgEditor.loadFromURL(file).then(function () {
         var svg = document.getElementById("svgID");
         code continues....
    }, function (error) {
        // load error handling
    }).then(callback.bind(this));
}

fileInstance = new FileLoad(url, callback);

Edit: The function changed its signature with version 4.0.0. For version 3.2.0 and older, you need to pass a callback function in a config object:

function FileLoad(file) {
    svgEditor.loadFromURL(file, {
        callback: function (success) {
            if (success) {
                 var svg = document.getElementById("svgID");
                 // code continues....
            } else {
                // load error handling
            }
        }
    });
}

Upvotes: 1

Related Questions