javascript - EmberJS : The best way to reload controller's model based on another property?

标签 javascript ember.js ember-data

根据另一个属性为当前 Controller 重新加载模型的最佳方法是什么?

例如:我有一个后 Controller 。作者只能发表一篇文章。如果 currentAuthor 属性更改,我想重新加载创建后的表单。

我试过这种方式:

App.PostEditController = Ember.ObjectController.extend
  modelReloadNeeded: Ember.observer((obj, keyName) ->
    postId = @get('currentAuthor').get('post_id')

    if postId?
      @set('model', @store.find('post', postId))
  , 'currentAuthor.post_id'
  )

它重新加载所有内容,但返回的不是真实模型,而是 promise。 而且它看起来不像是惯用的 Ember 解决方案。

也许有更好的方法来解决这个问题?

最佳答案

我相信重新加载 Controller 模型等同于重新进入相​​同的路线。所以从 2.12.0 开始有很多可能性,

  1. 您可以将操作发送到调用refresh 在当前路线上

Refresh the model on this route and any child routes, firing the beforeModel, model, and afterModel hooks in a similar fashion to how routes are entered when transitioning in from other route.

  1. 您可以在 url 路径中指定动态段,并在 Route 或 transitionToRoute 中使用 transitionTo 方法在 Controller 中。
Router.map(function() {
  this.route('posts');
  this.route('post', { path: '/post/:post_id' });
});

and you can say this.transitionTo('post',123) this will refresh the current route and you will get 123 as params in model hook.

https://guides.emberjs.com/v2.12.0/routing/defining-your-routes/#toc_dynamic-segments http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo

  1. You can specify the queryParams in the route and with refreshModel as true.
    import Ember from 'ember';
    export default Ember.Route.extend({
      queryParams: {
        postId: {
          refreshModel: true
        }
      },
      model(params) {
        //You will get query parameters value from the url through params.postId 
        return this.get('store').query('article', params);
      }
    });

You can even mention the same postId as queryParams in controller. it will refresh the route when you set postId property.

import Ember from 'ember';
export default Ember.Controller.extend({
  queryParams: ['postId'],
  postId: null, //here is the default value. default value will not be shown in URL.
});

https://guides.emberjs.com/v2.12.0/routing/query-params/#toc_opting-into-a-full-transition

关于javascript - EmberJS : The best way to reload controller's model based on another property?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18672216/

相关文章:

javascript - Youtube:this.f.isExternalMethodAvailable 不是一个函数

javascript - Ember onClick 事件未完成操作

javascript - Ember.js 模型保存不向服务器发送数据

javascript - 如何在 ember.js 1.6.x 中创建无路由子状态

javascript - 在 Ember.js 中创建和读取记录

javascript - 单击以加载远程数据的挂起 Bootstrap 模式

php - 如何将浏览器(Firefox、Safari、Opera、Chrome)中的书签导入您的应用程序?

javascript - 是否有任何高阶函数可以从 javascript 中的对象数组返回对象?

javascript - 使用 emberjs 选择 View 填充选择框

ember.js - 从后端接收更新数据后索引 View 不刷新