最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Exclude model properties when saving with Ember-Data - Stack Overflow

programmeradmin3浏览0评论

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

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() {
        });
      });
    }
  }
});
Share Improve this question edited Jan 31, 2014 at 19:19 Zach Shallbetter asked Jan 31, 2014 at 4:48 Zach ShallbetterZach Shallbetter 1,9116 gold badges23 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

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

You can override the serializeAttribute function on the Serializer:

App.PhotoSerializer = DS.DjangoRESTSerializer.extend({
    serializeAttribute: function(record, json, key, attribute) {
        if (attribute.name !== 'image') {
            this._super(record, json, key, attribute);
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论