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 ='productMaster', {
    brand: context.get('brand'),
    category: context.get('category'),
    cover: context.get('cover'),
    desc: context.get('desc')

productMaster.get('tags').pushObjects(context.get('tags')); () {
}).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'productMaster')

{ productMasters: 
   [ { category: '继电器',
       tags: ['538b23290b2f42bd464cec3e'],
       brand: '浙江泰华',
       cover: '',
       products: ['538b2374b11e91fcdfcbff33'],
       images: ['538b2374b11e91fcdfcbff32'],
       id: '538b2373b11e91fcdfcbff31',
       desc: 'asdfasdfasdfasdfasdf' } ],
   [ { url: '',
       id: '538b2374b11e91fcdfcbff32' } ],
   [ { sku: 100,
       pattern: 'HH52P-AC220V',
       brand: '浙江泰华',
       id: '538b2374b11e91fcdfcbff33',
       deadline: 4,
       price: 1000,
       unit: '只' } ],
   [ { 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);;
this.get('productsRemoved').forEach(function (product) {;
productMaster.get('images').forEach(function (image) {
 if (image.get('isDirty')) {
     image.set('productMaster', productMaster);;
this.get('imagesRemoved').forEach(function (image) {;
this.set('productsRemoved', Ember.A());
this.set('imagesRemoved', Ember.A()); () {
    context.set('files', Ember.A());

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

   {   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.