简体   繁体   English

Ember 从组件发送数据到路由并刷新它

[英]Ember send data from component to route and refresh it

I have a route in my Ember app that looks for an email in the URL as a query param and loads data based off of it.我的 Ember 应用程序中有一个路由,它在 URL 中查找电子邮件作为查询参数,并根据它加载数据。

export default Ember.Route.extend({

  queryParams: {
    email: true
  },

  actions: {
    complete (data) {
      this.set('model', data)
    }
  },

  model ({ email }) {
    return this.store.queryRecord('user', { email })
  }

})

The data and action from this route is getting passed to a user-form component:来自此路由的数据和操作正在传递给user-form组件:

{{user-form model=model addUser=(route-action "complete")}}

And then the component itself.然后是组件本身。 I'd like to be able to send data from this component to the route, which in turn updates the model:我希望能够将数据从此组件发送到路由,从而更新模型:

export default Ember.Component.extend({

  store: Ember.inject.service(),

  email: null,

  actions: {
    async submit() {
      const store = this.get('store')
      const email = this.get('email')
      try {
        await store.unloadAll('user')
        const data = await store.queryRecord('user', { email })
        await this.get('addUser')(data)
      } catch (error) {
        console.log(error)
      }
    }
  }

})

What I want to happen is that when the data is sent from the component to the route action, the model is updated with the new user.我想要发生的是,当数据从组件发送到路由操作时,模型会使用新用户进行更新。 It should only have one record at a time (hence the store.unloadAll('user') .它一次应该只有一个记录(因此是store.unloadAll('user')

Currently I'm not sure how to have a route that can load data via query params as well as data being sent to it from a component.目前我不确定如何拥有可以通过查询参数加载数据以及从组件发送到它的数据的路由。

  1. Manually setting model data in complete function is not the right way to refresh route.complete功能中手动设置model数据不是刷新路由的正确方法。
  2. Component sending data to set model is not the right way refresh route.组件将数据发送到设置model不是正确的刷新路线。

I would encourage you to use queryParams with refreshModel:true我鼓励你使用queryParamsrefreshModel:true

I modified your code, you please give it try.我修改了你的代码,你试试看。

Route file,路由文件,

export default Ember.Route.extend({
    queryParams: { email: { refreshModel: true } },
    model({ email }) {
        return this.store.queryRecord('user', { email })
    }
})

controller file,控制器文件,

export default Ember.Controller.extend({
    queryParams: ['email'],
    email: true, //default value
    actions: {
        updateEmail(email) {
            this.set('email', email) //setting email property will refresh route with new value
        }
    }
});

component file,组件文件,

export default Ember.Component.extend({
    email: null,
    actions: {
        submit() {
            this.get('updateEmail')(this.get('email'));
        }
    }
})

while including the component,在包含组件的同时,

{{user-form model=model updateEmail=(action 'updateEmail')}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM