【问题标题】:record not added to model when i call save() on a new record in Ember-Data当我在 Ember-Data 中的新记录上调用 save() 时,记录未添加到模型中
【发布时间】:2013-07-08 03:05:43
【问题描述】:

使用 Ember-Data 0.13-59 和 Ember 1.0.0 RC 6(来自入门套件)

问题:save() 到由App.Userstat.createRecord({ ... }) 创建的新记录时,服务器获取POST 并成功返回id,但新记录在@987654325 中不可用@模型。

为了更好地理解示例:这是一个测验应用程序(用于多项选择题)。每个问题都有几个选项,当用户选择一个选项时,他们对相应问题的选择存储在一个模型中,App.Userstat

对于每个问题,应用都需要知道用户是否已经回答了这个问题,或者它是否是新问题。

我将计算属性用作settergettersetter 在用户选择一个选项时被调用(选项的值被传递给计算属性)。首先,它检查用户当前问题的记录是否存在。如果它不存在,它将创建一个新记录。如果确实存在,它应该只发出带有更新内容的PUT 请求。

代码更新(7 月 8 日上午 11 点)

App.UserstatsController = Ember.ArrayController.extend();

App.QuestionController = Ember.ObjectController.extend({
  needs: "userstats",

  chosen = function(key, value) {
    // getter
    if(value === undefined) {

       // something goes here

    // setter
    } else {

      // the question.id is used to compare for an existing record in Userstat mdoel
      var questionId = this.get('id');
      var questionModel = this.get('model');

      // does any Userstat record belong to the current question??
      var stats = this.get('controllers.Userstats');
      var stat = stats.get('model').findProperty('question.id', questionId);

      // if no record exists for stat, means user has not answered this question yet...
      if(!stat) {

        newStat = App.Userstat.createRecord({
          "question" : questionModel,
          "choice" : value       // value passed to the computed property
        )}

        newStat.save();                     // I've tried this
        // newStat.get('store').commit();   // and this
        return value;

      // if there is a record(stat) then we will just update the user's choice
      } else {
        stat.set('choice', value);
        stat.get('store').commit();
        return value;
    }
  }.property('controllers.Userstats')

无论我设置多少次chosen,它总是会发送POST(与仅发送 PUT 请求的更新相反),因为它从不会第一次将记录添加到模型中。

为了进一步演示,在计算属性的setter 部分,当我输入这段代码时:

var stats = this.get('controllers.Userstats')
console.log stats 

Userstats 控制器显示所有以前存在的记录,但不显示新提交的记录!

save()commit()之后怎么新记录不可用???

谢谢:)

编辑

也许这与我向单一模型 App.Userstat 添加记录有关,然后当我查找它时,我正在使用作为数组控制器的 UserstatsController 进行搜索???

【问题讨论】:

  • 这就是你的全部代码吗?好像少了什么东西?和下面提到的intuitivepixel 一样,有一些错别字(比如createRecord 缺少右括号......)。无论如何,您似乎从未定义要传递给新记录的 question 变量?而且您永远不会将该新记录添加到Userstats 控制器,因此在其模型上使用findProperty 时它永远不会找到它......您可能也必须提交新的newStat
  • 感谢您的关注!我遗漏了重要的部分。我一定是在编辑或其他东西时覆盖了它们(已经很晚了)。我更新了代码以反映我的真实代码和我所经历的行为 - 即无法在 Userstats 控制器上找到最近提交的 .findProperty 记录

标签: ember.js ember-data


【解决方案1】:

不知道是不是笔误,但是计算的属性定义错误,应该是这样的:

App.QuestionController = Ember.ObjectController.extend({
  needs: 'userstats',
  choice: 'controllers.userstats.choice',
  chosen: function(key, value) {
    ...
  }.property('choice')
  ...
});

property() 中,您还应该定义在发生更改时触发计算属性的属性。这样如果choice 发生变化,chosen cp 就会被触发。

如果有帮助请告诉我。

【讨论】:

  • 感谢您发现这一点!我的问题中有一些错别字。我的真实代码确实有一个property()。请查看更新的代码。我重命名了 var questionModel ,添加了 .save().property()。这些已经在我的代码中,所以问题一定出在其他地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2020-09-24
  • 2018-02-25
  • 1970-01-01
相关资源
最近更新 更多