ember data 1.0.x not saving hasMany relationship when bidirectional

廉价感情. 提交于 2019-12-10 20:29:31

问题


One of the hasMany relationships isn't POSTing back to the server. How are you supposed to model a bidirectional relationship?

Here are the pertinent objects:

Encompass.Selection = DS.Model.extend({
  text: DS.attr('string'),
  submission: DS.belongsTo('submission', {inverse: 'selections'}),
});

Encompass.Submission = DS.Model.extend({
  shortAnswer: DS.attr('string'),
  selections: DS.hasMany('selection'),
  testing: DS.hasMany('folder'),
  workspaces: DS.hasMany('workspace'),
});

and a controller action:

testing2: function() {
  var submission = this.get('model');
  console.log(submission.get('selections.length'));
  var newSelection = this.get('store').createRecord('selection', {
    text: 'testing2 selection' + new Date().getMilliseconds(),
    submission: submission,
    coordinates: 'bogus coords',
    workspace: this.get('currentWorkspace')
  });
  //newSelection.save();
  console.log(submission.get('selections.length'));
  submission.save();
},

When I hit the testing2 action, the console shows the submission has 1 selection at first, then a second. The save() method triggers a post, but it's missing the selections object:

  {
    submission: {
      shortAnswer: "short", 
      testing:     [],
      workspaces:  ["5271d0147205f15e31000001"]
    }
  }

I've tried dropping and adding the inverse mappings. The strange part is the other hasMany relationships work.

The only thing I've found that works, is dropping the submission field from Selection.


回答1:


I've got the same problem. I tried removing the DS.belongTo statement with no effect. I used the model's "save" function to trigger a message to the server. I'm wondering whether there's another way of triggering that may result in a different message (with the hasMany records) being sent to the server.




回答2:


Here is one of the cases where hasMany isn't sent back.

We sometimes see the json serialized from a save() operation not contain the data we just set which was really puzzling.

selection.set('workspace', workspace)
selection.save()

The json might look like:

{ 
 id: 02314892038,
 workspace: undefined,
 isTrashed: false
}

The workspace is being set, just not right now. There is a promise to set it and we should just wait for it if it's needed by the backend.

selection.set('workspace', workspace);
selection.get('workspace').then(function(){
  selection.save();
});

So essentially you might see non-deterministic behavior:

  • works for this method/why not here
  • works for me
  • used to work

This doesn't only apply to a save following a set. I've seen cases where the data was loaded from earlier but for some reason the relationship wasn't complete.

I'm not sure why this can't serialize the ID for an unfulfilled relationship (we always know the ID and that's all we need for the JSON).



来源:https://stackoverflow.com/questions/19716852/ember-data-1-0-x-not-saving-hasmany-relationship-when-bidirectional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!