-1

I want use Backbone.save the model,and the model's nest data need to be filter,so i use

model.save(null,{
    success: ...,
    error:...,
    data: {
        id:null,
        name:'myname',
        nestmodel: {
            id:'xx'/*Other data i don't need it,so just id column*/
        }
    }
}

And I don't want to use patch HTTP METHOD. Because i just add a new model,not change part data.

And i don't want to post some nestmodel data,Because it's to big and i just want the id is ok.

And nestmodel just need the id.

I have read Exclude model properties when syncing (Backbone.js) and Backbone.js/express.js parameters for model.save()

There is a way to solve that problem.

That's whole my code:

 sync: function(method, model, options) {
  var data, orderSuiteItems;
  if (method === 'create') {
    options.url = this.url;
  } else {
    // MUST setting the url .options's url is undefined
    options.url = this.url + this.idUrl(this.get('id'));
  }
  // IF `create` or `update` , pick the we need properties
  if (method === 'create' || method === 'update') {
    orderSuiteItems = [];
    if (this.has('orderSuiteItems')) {
      // Because the `dishes` and `dishesUnitPrice` have a lot of prop,
      // Each of `dishes` or `dishesUnitPrice` may each have 10K data
      orderSuiteItems = _.map(this.get('orderSuiteItems'), function(osi) {
        return {
          id: osi.id,
          qty: osi.qty,
          servingQty: osi.qty,
          confirmQty: osi.confirmQty,
          deleted: osi.deleted,
          orderItem: _.pick(osi.orderItem, 'id'),
          dishes: _.pick(osi.dishes, 'id'), // HAVE a large prop 
          dishesUnitPrice: _.pick(osi.dishesUnitPrice, 'id'), // HAVE a large prop 
          orderItemStatus: osi.orderItemStatus,
          piece: osi.piece
        };
      });
    }
    data = {
      id: this.get('id'),
      order: this.get('order'),
      price: this.get('price'),
      dishes: _.pick(this.get('dishes'), 'id', 'isSuite'),
      dishesUnitPrice: _.pick(this.get('dishesUnitPrice'), 'id'),
      qty: this.get('qty'),
      servingQty: this.get('servingQty'),
      confirmQty: this.get('confirmQty'),
      sum: this.get('sum'),
      orderSuiteItems: orderSuiteItems,
      orderItemStatus: this.get('orderItemStatus')
    };
    // Setting attrs with pick data.
    options.attrs = data;
    return Backbone.sync(method, model, options);
  } else {
    return Backbone.sync(method, model, options);
  }
}
sjbwylbs
  • 151
  • 14

1 Answers1

-1

I hope you just put the data option for the sake of the example's clarity.

Anyway, how about using unset to remove your attribute just before using Model#save? Re-set it just afterwards.
Another solution would be to override the Model#save method.
You could also shadow the same method by defining it as a property and not in the prototype (that'd give you the opportunity to switch back).

Solution #1 or something similar would be the easiest. Solution #2 may be more, let's say, risky, but would have maybe less boilerplate. I would use the #3 only in some very specific case (can't even think about one as of now) that would include: object being a singleton (because we're not using the prototype)(or only in a limited number), need to switch the 2 modes a lot, better to have only 1 method.

Edit:
Solution #1:

var nestedModel = myModel.get('nestmodel');
myModel.save('nestmodel', nestedModel.id, {silent: true});
myModel.set('nestmodel', nestedModel, {silent: true});

I added the silent flag as I don't know if you're listening to your nestmodel attribute's changes. I'll add code for the other solutions if this one doesn't suit you.

Loamhoof
  • 8,293
  • 27
  • 30