1
\$\begingroup\$

I have a view that is dynamically generated from a JSON file on Angular, and I am using JSON-Patch (RFC6902) to generate patches when the user modifies its values.

I start observing the JSON since the very beginning of the application and stop when the user want to submit the changes. My problem is that on the meantime there will be some internal operations with the object I don't want to capture.

So whenever this situation occurred, I created another observer on a part of the object to capture what I do not want to send and ignore it, so far so good. But this situations become more common than what I thought, so I tried to generalize it. I've used a bit of this link to base myself.

I designed this function on the service responsible for observing the JSON and generating patches, it basically saves the operations I don't want in a blacklist to remove them after.

this.ignoreThisOperations = function (objToObserve, func) {
    var observer = jsonpatch.observe(objToObserve);

    func.apply(this, arguments);

    var trashPatch = jsonpatch.generate(observer);

    if (!_.isEmpty(trashPatch)) {
        this.addToBlackList(trashPatch);
    }
};

And whenever I need to ignore an operation I use it like:

patchGenerationService.ignoreThisOperations(obj, function () {
     obj.entries = []
});

It works perfectly and helped me on organizing the code. But I don't know how good this is, or if there a better/simpler way.

Also, I didn't use anything from angular to implement this, is there anything that could help me? I searched a bit but maybe I did not dig enough to find anything.

\$\endgroup\$
  • \$\begingroup\$ "But I don't know how good this is, or if there a better/simpler way." -- It's hard for me to tell as well, because the description of the problem is so abstract. I still don't really understand why you are implementing a view in this way, or what your application might look like. Your design might be brilliant or over-engineered, depending on details. A working fiddle, or screenshots, usually helps and makes giving feedback much easier. \$\endgroup\$ – Jonah Dec 11 '15 at 18:24
  • \$\begingroup\$ I think that will be not possible. The application is to complex to compact in a fiddle. The only thing I care here is the way I am wrapping the function. \$\endgroup\$ – Patrick Bard Dec 12 '15 at 1:38
  • \$\begingroup\$ The thing is, code review is about making your code readable. Making your code your readable is about making your code reveal its intentions clearly. So being unsure about your intentions makes it hard to critique the code. On a purely abstract level, your snippet seems fine, if it's working. \$\endgroup\$ – Jonah Dec 12 '15 at 4:07
  • \$\begingroup\$ That's why I detailed (well enough, I hope) it in text, since the only point I am questioning is if the way I implemented is ok or if there's a better way to wrap a funtion into another one in javascript. \$\endgroup\$ – Patrick Bard Dec 12 '15 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.