简体   繁体   English

如何将控制器数据传递到组件中并在ember js中备份

[英]how to pass controller data into component and back up in ember js

I have an ember 2.13 app that has a users route fetching a users model. 我有一个ember 2.13应用程序,该应用程序具有用户路由来获取用户模型。 So in the users template I am showing all the users fetched. 因此,在用户模板中,我将显示所有获取的用户。 I also have a users controller that feeds data to an edit form when a user is clicked. 我还有一个用户控制器,当单击用户时,该控制器会将数据提供给编辑表单。 I'm using the user controller to do this. 我正在使用用户控制器来执行此操作。

All the code is in a single template but I would like to put the rendering user list and edit user form in two separate components. 所有代码都在一个模板中,但是我想将呈现用户列表和编辑用户表单放在两个单独的组件中。 Let's say I make a render-single-user and edit-user templates. 假设我制作了一个渲染单用户和编辑用户模板。 So when a user clicks on a user in rendered using the render-single-user component, data of this user should show up in the dom rendered by edit-user component. 因此,当用户单击使用render-single-user组件渲染的用户时,此用户的数据应显示在edit-user组件渲染的dom中。

The problem is a component doesn't have access to the route's model or the app's store. 问题是组件无法访问路线的模型或应用程序的商店。

I would be grateful if someone would tell me how to pass the model and store from controller to the components and also between the render-single-user and edit-user components. 如果有人告诉我如何将模型从控制器传递到组件以及在渲染单用户组件和编辑用户组件之间进行传递,我将不胜感激。 And when a user clicks update/save the modified data should travel up to the controller and save the record to the store consequently making a post request to the server. 并且,当用户单击更新/保存时,修改后的数据应传输到控制器,并将记录保存到存储中,从而向服务器发出发布请求。

Screenshots attached. 截图。

User View 使用者检视

在此处输入图片说明

Clicked User 点击的用户

在此处输入图片说明

This answer applies to versions 2.xx and was written as of 2.15. 此答案适用于版本2.xx,是从2.15版本开始编写的。

It sounds like you're just getting started in Ember. 听起来您刚刚开始使用Ember。 Welcome! 欢迎! I highly encourage you to do the Tutorial and read The Guides . 我强烈建议您阅读 教程并阅读《指南》 They are an investment - things will go much faster and be much easier for you afterwards. 他们是一项投资-事情会变得更快,之后对您来说会容易得多。 Everything I'm about to tell you is available there. 我要告诉你的一切都可以在那儿找到。 They are not skippable. 它们不可跳过。

In your users route: 在您的用户路线中:

model() {
   return this.store.findAll('users')
}

In your users controller: 在您的用户控制器中:

actions: {
  consoleLogger(thingToLog) {
    console.log(thingToLog)
  }
}

Data and actions are passed from controllers and routes down to components. 数据和操作从控制器传递并向下路由到组件。 This is done through the handlebars templates. 这是通过车把模板完成的。

Generic example of passing actions, model, and variables to a component: 将动作,模型和变量传递给组件的一般示例:

{{some-component 
   someControllerVariable=someControllerVariable
   mode=model 
   consoleLogger=(action "consoleLogger")}}

Then in your component, you can either use the action like this: 然后,在组件中,您可以使用以下操作:

<button {{action consoleLogger "string to log"}}>Log it</button>

Or use it in your component js like this: 或像这样在组件js中使用它:

this.get('consoleLogger')("string to log");
// this.get retrieves the function and then we call it with an argument, kind of like how you'd do someOtherFunction() in plain js

Since you need to show a list of helpers, you will need to use the each helper in your route: 由于需要显示助手列表,因此需要在路线中使用每个助手

{{#each users as |user|}}
       {{user-list-item 
       someControllerVariable=someControllerVariable
       user=user 
       consoleLogger=(action "consoleLogger")}}
{{/each}}

When the action is used in your component, you can pass records as arguments. 在组件中使用操作时,可以将记录作为参数传递。 They'll go to the controller action and that's where you should do your POST/PATCH/DELETE type operations like save(). 他们将转到控制器操作,在这里您应该执行POST / PATCH / DELETE类型的操作,如save()。

Good luck :) 祝好运 :)

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

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