Kosmetika
Kosmetika

Reputation: 21304

Backbone.js - make POST request without saving a model

I'm wondering is there a possibility to make this.model.save(data, options) in Backbone without writing data to model?

I have a server endpoint where I need to POST json { field1: 'bbb', field2: 'aaa' } - server writes these fields into db like list: [{ field1: 'bbb', field2: 'aaa' }].

The problem is that Backbone adds these fields to model as well. And I have model like { id: '111', field1: 'bbb', field2: 'aaa', list: [{ field1: 'bbb', field2: 'aaa' }] } and that's wrong..

Is there a way to make save without writing send data to model? Or maybe simply use jquery $.post method and set model on success?

Upvotes: 0

Views: 597

Answers (2)

Dmitry Masley
Dmitry Masley

Reputation: 525

  1. You can define sync function and make ajax there
  2. You can define parse function and write something like this

    parse:function(data) { return data.list; }

2 is the better approach. If setting some field will affect another one, server will return it and you will always have actual data in the model.

Upvotes: 1

lecstor
lecstor

Reputation: 5707

I think the easiest way to fix this would be to either change the data returned by the server, or override the parse method in your model and remove the extra attribute.

A second thought would be to give your moel a single attribute of list and set that with the data dict and have the server return the same. This won't be 100% compatible with general Backbone model usage though.

gotta go..

Upvotes: 1

Related Questions