Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude model properties when saving with Ember-Data

I have a model that has an image property. When saving images I would like to not Post that property to the endpoint. I was thinking perhaps I could .set the changes on everything beside the image property then save. But save still posts everything.

Also, my Adapter supports PATCH so I can successfully save portions of the model.

My model

App.Photo = DS.Model.extend({
  title: attr(),
  description: attr(),
  image: attr(),
  authors: hasMany('author'),
  imageURL: function() {
    return document.location.origin + '/media/' + this.get('image');
  }.property('image'),
  created: attr('date')
});

My Controller

App.PhotoController = Ember.ArrayController.extend({
  actions: {
    save: function() {
      this.get('model').save().then(function(success) {
        self.transitionToRoute('photos').then(function() {
        });
      });
    }
  }
});
like image 856
Zach Shallbetter Avatar asked Nov 30 '22 19:11

Zach Shallbetter


1 Answers

I think since this PR on Ember there is a better way to handle the exclusion of an attribute.

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  attrs: {
    image: {serialize: false}
  }
});

If you have more than one attribute to exclude the above code looks cleaner. Also have a look on the documentation of DS.JSONSerializer

like image 147
webpapaya Avatar answered Dec 06 '22 20:12

webpapaya