Ember-数据:模型刷新于 DS.Store.createRecord

Ember-data: model refresh on DS.Store.createRecord

恩伯斯特!

我想弄清楚为什么 我的模型在创建新记录并将其保存到商店后没有刷新。

我的路线计算 model 如下:

model: function (params) {
  var postID = params.post_id,
      userID = this.get('session.currentUser.id');

  var post = this.store.findRecord('post', postID) ;

  var followings = this.store.query('post-following', {
      filter: { post: postID }
  }) ;
  var userFollowing = this.store.queryRecord('post-following', {
      filter: { post: postID, user: userID }
  }) ;

  return new Ember.RSVP.hash({
      post:          post,
      followings:    followings,
      userFollowing: userFollowing
  });
}

然后我的模板呈现一个列表一个按钮:

{{#each model.followings as |following|}}
    ...
{{/each}}

{{#if model.userFollowing}}
    <button {{action 'follow'}}>Follow</button>
{{else}}
    <button {{action 'unFollow'}}>Unfollow</button>
{{/if}}

和我的控制器creates/deletes相关的post-following记录:

actions: {
    follow: function () {
        var user = this.get('session.currentUser'),
            post = this.get('model.post') ;

        this.store.createRecord('post-following', {
            user: user,
            post: post
        }).save();
    },
    unFollow: function () {
        this.get('model.userFollowing').destroyRecord() ;
    }
}  

当我点击 [Follow] 按钮时:

当我(然后刷新页面)单击 [Unfollow] 按钮时:

你知道我做错了什么吗?
会不会是我的负载有问题?


编辑:已解决!

嗯,听起来我对 ember 的期望太高了。

框架不会在 store.createRecord('post-following', {...}) 调用时自动更新我的 post-followings 数组。

然后我调整我的控制器逻辑以"manually"更新我的模型:

// in follow action…
userFollowing.save().then( function(){
    var followings = store.query('post-following', {
        filter: { post: postID }
    }).then( function (followings) {
        _this.set('model.userFollowing', userFollowing);
        _this.set('model.followings', followings);
    }) ;
});
// in unFollow action…
userFollowing.destroyRecord().then( function () {
    _this.set('model.userFollowing', null);
    _this.notifyPropertyChange('model.followings') ;
}); 

请注意 我的后端 API 设计受到了@duizendnegen 的批评(见评论)。 this article.

中的更多最佳实践

感谢大家的帮助!!!
布鲁

您的型号在您进入该页面时即已设置。进行更改时,您的模型不会更改。当您销毁记录时列表会更新的唯一原因是因为它根本不存在了。单击关注按钮或取消关注按钮后重新加载模型,或手动更改 list/button.

的值

您的问题是您没有重新设置 属性 model 以指向新创建的对象。您总是访问相同的 model 属性,即使在创建新的之后也是如此。

首先要注意的是,在路由中的 model 挂钩之后,会调用 setupController 挂钩执行:

controller.set('model', resolvedModel)

意味着你的控制器上的 model 属性 默认情况下,每次加载路由时都会设置(model 挂钩解析)。但是,这 不会在您创建新记录后发生,因此您必须明确地这样做:

let newModel = this.store.createRecord('post-following', {
  user: user,
  post: post
})

// since model.save() returns a promise
// we wait for a successfull save before
// re-setting the `model` property
newModel.save().then(() => {
  this.set('model', newModel);
});

为了更清晰的设计,我还建议您为 model 属性 创建一个别名,以更具体地描述您的模型或覆盖 setupController 的默认行为,如果您还在控制器上进行一些初始设置。所以要么:

export default Ember.Controller.extend({
  // ...

  blog: Ember.computed.alias('model') // more descriptive model name   

  // ...
});

或:

export default Ember.Route.extend({
  // ...

  setupController(controller, resolvedModel) {
    controller.set('blog', resolvedModel); // more descriptive model name 
    // do other setup
  }

  // ...
});

对于这类问题,有一个更小的、可复制的问题真的很有帮助(例如通过 Ember Twiddle)

从根本上说,新的 post-following 记录与 filter 不匹配:它针对属性 { post: 123 } 进行过滤,并且您的 post-following 对象包含行中的内容{ post: { id: 123, name: "" } } 个。此外,您的 post-following 对象不包含名为 filter 的 属性 或它可能是什么 - 即它对服务器执行的查询不同于您要在客户.

我的方法是,作为对 followunfollow 操作的响应,更新 modeluserFollowingfollowings.