0

I am implementing a drag'n'drop interface in js which has two tables of objects and a single object as source or destination. To execute the "move" operation, I thought of using these two class methods:

    _getVar(id) {
        var ind;
        switch (id[0]) {
            case 'c':
                return this.clipboard;
            case 's':
                ind = Number(id.substr(1)) - 1;
                return this.SynthPatches[ind];
            case 'f':
                ind = Number(id.substr(1)) - 1;
                return this.FilePatches[ind];
        }
    }

    
    move(from, to) {
        var fv = this._getVar(from);
        var tv = this._getVar(to);
        if (fv == undefined) return "Source empty";
        if (tv == undefined) return "Destination undefined";
        tv = fv;
        return "Ok";
    }

This does not work. fv and tv are local references to the correct objects. The reference fv is copied to tv and then both are forgotten. I would like something like a deep copy in python, but it looks like the only solution is to inline _getVar() nine times, which makes for really ugly code.

2
  • tv = fv; that will not behave as you expect, it will just assign the local variable tv the value of fv, so tv will no more point to it's original object, and nothing happens outside of that function, you need a refference example: mainObj.tv = fv; or mainObj[index].tv = fv; Commented Aug 26, 2020 at 8:04
  • The challenge here is, that there is no single "mainObj". Instead it is _getVars task to select one of three possible mainObjects (this.SynthPatches, this.FilePatches or this.clipboard). Commented Aug 26, 2020 at 10:53

1 Answer 1

1

The easiest way to reduce duplication here is probably to add an optional second parameter to the function and allow it to set as well as get, e.g.:

_getOrSetVar(id, value) {
    var ind;
    switch (id[0]) {
        case 'c':
            if (value !== undefined) this.clipboard = value
            return this.clipboard;
            break;
        case 's':
            ind = Number(id.substr(1)) - 1;
            if (value !== undefined) this.SynthPatches[ind] = value
            else return this.SynthPatches[ind];
            break;
        case 'f':
            ind = Number(id.substr(1)) - 1;
            if (value !== undefined) this.FilePatches[ind] = value
            else return this.FilePatches[ind];
            break;
    }
}

And perform your update like this:

this._getOrSetVar(to, this._getOrSetVar(from))
Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution! The cases now need break statements and I need to handle the "undefined" situations with exceptions, but the idea is great.
Good catch, I've added breaks. The undefined checks are for when you're using the method to get the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.