# Wrapping a function in other function to generate Json Patches

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)) {
}
};


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.

• "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. – Jonah Dec 11 '15 at 18:24
• 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. – Patrick Bard Dec 12 '15 at 1:38