javascript EmberJS:基于另一个属性重新加载控制器模型的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18672216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:49:54  来源:igfitidea点击:

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

javascriptember.jsember-data

提问by somebody32

What is the best way to reload model for a current controller based on another property?

基于另一个属性为当前控制器重新加载模型的最佳方法是什么?

For example: I have a post controller. Author can have only one post. I want to reload post creating form if currentAuthor property changes.

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

I've tried that way:

我试过这样:

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

It reloads everything, but returns not a real model, but promise. And also it not looks like an idiomatic Ember solution.

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

Maybe there is any better way to approach this problem?

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

回答by Ember Freak

I believe reloading controllers model is equivalent to re-entering the same route. so there are lots of possibilities as of 2.12.0,

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

  1. You can send action to route to call refreshon the current route
  1. 您可以向路由发送操作以在当前路由上调用刷新

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.

刷新此路由和任何子路由上的模型,以与从其他路由过渡时输入路由的方式类似的方式触发 beforeModel、model 和 afterModel 挂钩。

  1. You can specify dynamic segmentsin url path and use transitionTo method in Route or transitionToRoutein controller.
  1. 您可以指定动态段的URL路径,并使用transitionTo方法在路由或transitionToRoute的控制器。
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.

您可以说this.transitionTo('post',123)这将刷新当前路线,您将在模型挂钩中获得 123 作为参数。

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

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 queryParamsin the route and with refreshModel as true.
  1. 您可以在路由中指定 queryParams,并将 refreshModel 设置为 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 postIdproperty.

您甚至可以在控制器中提及与 queryParams 相同的 postId。当您设置postId属性时,它将刷新路线。

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

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

回答by chriswessels

I'm nervous to be giving you a solution to this problem, because I'm not sure you're approaching it correctly, however:

我很紧张为您提供解决此问题的方法,因为我不确定您是否正确地解决了这个问题,但是:

App.PostEditController = Ember.ObjectController.extend({
  modelReloadNeeded: function () {
    this.get('content').reload();
  }.observes('currentAuthor')
});

This should reload the PostEditController's content property and therefore update your templates whenever the content's currentAuthorproperty changes.

这应该重新加载PostEditController的内容属性,从而在内容的currentAuthor属性更改时更新您的模板。

回答by Jeremy Green

Something like this would find a different post and then set it as the content of the PostEditController.

像这样的事情会找到一个不同的帖子,然后将其设置为PostEditController.

App.PostEditController = Ember.ObjectController.extend({
  modelReloadNeeded: function () {
    // Find another post based on author id, tweak to suit your needs
    var otherPost = this.store.find('post',{ author_id : this.get('currentAuthor.id') })
    this.set('content', otherPost );
  }.observes('currentAuthor')
});

回答by somebody32

store.findwill return you a promise, so your should resolve it to avoid issues with no methods call

store.find会给你一个承诺,所以你应该解决它以避免没有方法调用的问题

App.PostEditController = Ember.ObjectController.extend
  modelReloadNeeded:(->
      postId = @get('currentAuthor').get('post_id')
      if postId?
        @store.find('post', postId).then (post) =>
          @set('content', post)
    ).observes('currentAuthor.post_id')