x不保存在双向时有很多关系

x不保存在双向时有很多关系

本文介绍了ember数据1.0.x不保存在双向时有很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中一个hasMany关系不是POST回服务器。你应该如何建立双向关系?

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

这里是相关的对象:

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'),
});

和控制器操作:

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

当我打了testing2动作,控制台显示提交有1个选择起初,然后一秒钟。 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.

推荐答案

其中hasMany不被发回。

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

我们有时会看到从一个save()操作序列化的json不包含我们刚刚设置的数据真的令人费解。 >

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()

json可能如下所示:

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:


  • 适用于此方法/为什么不在这里

  • 适用于我

  • 曾经工作

这不仅适用于一个集合后的保存。我已经看到从早期加载数据的情况,但由于某种原因,关系尚未完成。

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.

我不知道为什么这不能序列化ID为一个未实现的关系(我们总是知道ID,这就是我们需要的JSON)。

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

这篇关于ember数据1.0.x不保存在双向时有很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:36