gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:27:29 24 4
gpt4 key购买 nike

根据另一个属性为当前 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-segmentshttp://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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com