4

My data model is look like the following JSON structure (just example):

var post = {
  id: 123,
  title: 'Sterling Archer',    
  comments: [
    {text: 'Comment text', tags: ['tag1', 'tag2', 'tag3']},
    {text: 'Comment test', tags: ['tag2', 'tag5']}
  ]  
};

In Backbone side it represented as nested model that looks likes the following:

var PostModel = Backbone.Model.extend({
   parse: function (response) {
       if (response.comments) {
          response.comments = new Backbone.Collection(response.comments);
       }
       return response;
   }
});

var post = new PostModel(post, {parse: true});

I want to apply rfc6902 (JSONPatch) specification to my structure. But the problem here is that my structure is not pure JSON but nested model/collection units.

I need best practices about how could I patch my nested backbonejs structure like in official documentation examples:

Does anybody has experience in using JSON+Patch specification in your BackboneJS applications? Please share with us.

Thanks.

EDIT: here is the short example. Let's say I need to make some modifications on my post model like comment adding:

var op = [
  { "op": "add", "path": "/comments/2", "value":  {text: 'Comment test3', tags: ['tag4']}" }
] 

How could I do it with backbone:

post.appyPatch(op);

Is there any best practices or/and backbone extensions for do it?

3
  • Not clear on what you're trying to accomplish with PATCH, but it seems to me that you'll want to override the model's toJSON method to translate the structure back to valid JSON regardless.
    – kinakuta
    Commented Jul 22, 2014 at 7:40
  • Sorry, I've updated the post
    – Erik
    Commented Jul 22, 2014 at 9:30
  • 1
    The async method in Backbone does not support this type of request unfortunately. You could always extend it yourself. That would be the best practice IMHO. You could always look at this JSON patch library and get some cues.
    – AlexandruB
    Commented Jul 25, 2014 at 14:10

1 Answer 1

4
+200

I've created a working app here in Plunker using the shared code and json-patch.js library for applying the patch. I've extended PostModel with applyPatch method which applies the patch. Here is the applyPatch method code:

var PostModel = Backbone.Model.extend({
    ...    
    applyPatch: function(op) {
        var postStringify = JSON.stringify(this); // JSON string
        var postAttributesJSON = JSON.parse(postStringify); // JSON object. This is same as postAttributes
        var postPatched = jsonpatch.apply_patch(postAttributesJSON, op); // Patch applied
        var changed = this.changedAttributes(postPatched); // Changed attributes
        var self = this; 
        _.each(_.keys(changed), function(key) {
            if(key == 'comments') {
                self.get('comments').set(changed[key], {merge: true});
            }
        }); 
    }
});
3
  • 1
    Thanks for the answer. But I see the following problems in your code: 1. After this.set(postPatched) I lose comments Collection. 2. It's not efficient to replace whole model after applying patch. It could be better it we can update specific unit of nested model. In my example it's just add new model into 'comments' collection instead replacing one. Do you have any thoughts about it?
    – Erik
    Commented Jul 30, 2014 at 8:10
  • 1
    Sorry I missed out on nested collection. It may not be possible to have generalized functionality on nested collections. I've modified the app in Plunker. Let me know whether it helps. Commented Jul 30, 2014 at 11:33
  • Thanks for the reply, I thought that is possible to make it for any nested model without knowledge about these name (like 'comments').
    – Erik
    Commented Jul 30, 2014 at 13:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.