Questions about relationship model create, update, delete, Wonder If I am doing right

##Model definition.

Erp.ProductMaster = DS.Model.extend({
    brand: DS.attr('string'),
    category: DS.attr('string'),
    cover: DS.attr('string'),
    desc: DS.attr('string'),


    tags: DS.hasMany('productTag'),
    images: DS.hasMany('productImage'),
    products: DS.hasMany('product')
});

Erp.ProductTag = DS.Model.extend({
    name: DS.attr('string'),


    productMasters: DS.hasMany('productMaster')
});

Erp.ProductImage = DS.Model.extend({
    url: DS.attr('string'),


    productMaster: DS.belongsTo('productMaster')
});

Erp.Product = DS.Model.extend({
    brand: DS.attr('string'),
    pattern: DS.attr('string'),
    price: DS.attr('string'),
    deadline: DS.attr('string'),
    unit: DS.attr('string'),
    sku: DS.attr('string'),


    productMaster: DS.belongsTo('productMaster')
});

##create productMaster

var context = this;

var productMaster = this.store.createRecord('productMaster', {
    brand: context.get('brand'),
    category: context.get('category'),
    cover: context.get('cover'),
    desc: context.get('desc')
});

productMaster.get('tags').pushObjects(context.get('tags'));
productMaster.save().then(function () {
    productMaster.get('products').pushObjects(context.get('products'));
    productMaster.get('products').save();
    productMaster.get('images').pushObjects(context.get('images'));
    productMaster.get('images').save();
}).then(function () {
    context.set('brand', '');
    context.set('category', '');
    context.set('desc', '');
    context.set('cover', '');
    context.set('products', Ember.A());
    context.set('tags', Ember.A());
    context.set('images', Ember.A());
    context.set('files', Ember.A());
});

##findAll payload

Just call this.store.findAll('productMaster')

{ productMasters: 
   [ { category: '继电器',
       tags: ['538b23290b2f42bd464cec3e'],
       brand: '浙江泰华',
       cover: 'http://keptrans.b0.upaiyun.com/products/0a7d612510f64ede9b0fa1e28ce45471.jpeg',
       products: ['538b2374b11e91fcdfcbff33'],
       images: ['538b2374b11e91fcdfcbff32'],
       id: '538b2373b11e91fcdfcbff31',
       desc: 'asdfasdfasdfasdfasdf' } ],
  productImages: 
   [ { url: 'http://keptrans.b0.upaiyun.com/products/a6f2f3c455fc41498662775bda60e7ad.jpeg',
       id: '538b2374b11e91fcdfcbff32' } ],
  products: 
   [ { sku: 100,
       pattern: 'HH52P-AC220V',
       brand: '浙江泰华',
       id: '538b2374b11e91fcdfcbff33',
       deadline: 4,
       price: 1000,
       unit: '只' } ],
  productTags: 
   [ { name: 'HH52P',
       id: '538b23290b2f42bd464cec3e' } ] }

##update productMaster

var context = this;

var productMaster = this.get('model');
productMaster.get('products').forEach(function (product) {
    if (product.get('isDirty')) {
        product.set('productMaster', productMaster);
        product.save();
    }
});
this.get('productsRemoved').forEach(function (product) {
    product.save();
});
productMaster.get('images').forEach(function (image) {
 if (image.get('isDirty')) {
     image.set('productMaster', productMaster);
     image.save();
 }
});
this.get('imagesRemoved').forEach(function (image) {
    image.save();
});
this.set('productsRemoved', Ember.A());
this.set('imagesRemoved', Ember.A());
productMaster.save().then(function () {
    context.set('files', Ember.A());
});

In product’s put method, the server returns payload like this:

{
   product: 
   {   sku: 100,
       pattern: 'HH52P-AC220V',
       brand: '浙江泰华',
       id: '538b2374b11e91fcdfcbff33',
       deadline: 4,
       price: 1000,
       unit: '只' 
   }
}

Anything Wrong?

In product’s delete method, the server only return

{product: {id : '538b2374b11e91fcdfcbff33'}}

#Question #1

What should payload look like in delete request?

##Delete productMaster

While doing delete, I got confused. I got this error.

Assertion Failed: calling set on destroyed object

#Question #2

what should I do in delete? Should I destroy all the products and images, after success, I destroy productMaster?

You don’t need to attach (or return) a payload to a DELETE request. Just:

DELETE endpoint/goes/here/123

Should return either a status code 200 or 204. No JSON needed.

As far as cascading deletes, I usually handle that directly in the database, but I’m curious if anyone else does this client-side.

this looks like a good candidate for a stack-overflow post

Solved by delete relevant data on server side after invoke deleteRecord all client side’s relevant data.