EmberJS:基于另一个属性重新加载控制器模型的最佳方式? [英] EmberJS: The best way to reload controller's model based on another property?

查看:62
本文介绍了EmberJS:基于另一个属性重新加载控制器模型的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我已经尝试了这样的方式:

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

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

它重新加载所有内容,但不会返回真实的模型,而是承诺。
它也不像一个惯用的Ember解决方案。



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

解决方案

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


  1. 你可以发送动作路由调用刷新当前路线




刷新此路由和任何子路由上的模型,以类似的方式触发beforeModel,model和afterModel钩子,以便在从其他路由转换时输入路由。 / p>




  1. 您可以在url路径中指定动态段,并使用transitionTo方法路由或 transitionToRoute 在控制器中。



    Router.map(function(){
    this.route('posts');
    this.route('post',{path:'/ post /:post_id' });
    });
    ,您可以说 this.transitionTo('post',123)这将刷新当前路由,您将获得123作为模型钩子中的参数。


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. 您可以在路由中指定queryParams,并将refreshModel指定为true。



 
从ember导入Ember;
导出默认Ember.Route.extend({
queryParams:{
postId:{
refreshModel:true
}
},
模型(params){
//通过params.postId
返回this.get('store')将从url获取查询参数值query('article',params);
}
});

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

  import Ember从'ember'; 
导出默认值Ember.Controller.extend({
queryParams:['postId'],
postId:null,//这是默认值,默认值不会显示在URL中。
});

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


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.

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.

Maybe there is any better way to approach this problem?

解决方案

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

  1. You can send action to route to call refresh on the current route

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. You can specify dynamic segments in url path and use transitionTo method in Route or transitionToRoute in 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

这篇关于EmberJS:基于另一个属性重新加载控制器模型的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆