Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Ember property is passed


May 09, 2021 Ember.js Reference documents


Table of contents


1, pass parameters to the component

Each component is relatively independent, so the data required by any component needs to be passed to the component through its properties.

For example, the third point defined by the twenty-eight components of the Ember.js {{component item.pn post=item}} Getting Started Guide to the last time is to pass data to the foo-component or bar-component post. I f the component is called in index.hbs as follows, the rendered page is empty. {{component item.pn}} code for index.hbs to demonstrate the effect.

The parameters passed to the component are also dynamically updated, and the HTML rendered by the component changes as the parameters passed to the component change.

2, position parameters

The passed property argument does not have to specify the name of the argument. Y ou can not specify the name of the property parameter, and then get the corresponding value based on the location of the parameter, but specify the name of the location parameter in the component class corresponding to the component. For example, the following code:

Preparations:

  1. ember g route passing-properties-to-component
  2. ember g component passing-properties-to-component

Call the template of the component and pass in two position parameters, item.title item.body

  1. !-- apptemplatespassing-properties-to-component.hbs --
  2. {{#each model as item}}
  3. !-- 传递到组件blog-post第一个参数为数据的title值,第二个为body值 --
  4. {{passing-properties-to-component item.title item.body}}
  5. {{each}}

Prepare the data that needs to be displayed.

  1. approutespadding-properties-to-component.js
  2. import Ember from 'ember';
  3. export default Ember.Route.extend({
  4. model function() {
  5. return [
  6. { id 1, title 'Bower dependencies and resolutions new', body In the bower.json file, I see 2 keys dependencies and resolutionsWhy is that so },
  7. { id 2, title 'Highly Nested JSON Payload - hasMany error', body Welcome to the Ember.js discussion forum. We're running on the open source, Ember.js-powered Discourse forum software. },
  8. { id 3, title 'Passing a jwt to my REST adapter new ', body This sets up a binding between the category query param in the URL, and the category property on controllerarticles. }
  9. ];
  10. }
  11. });

Specify the name of the location parameter in the component class.

  1. appcomponentspadding-properties-to-component.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. 指定位置参数的名称
  5. positionalParams ['title', 'body']
  6. });

Note: The parameters specified by the propertypositional Params cannot be changed during the run time.

The component gets the data directly using the location parameter name specified in the component class.

  1. !-- apptemplatescomponentspassing-properties-to-component.hbs --
  2. article
  3. h1{{title}}h1
  4. p{{body}}p
  5. article

Note: The name of the obtained data must match the name specified by the component class, otherwise the data cannot be obtained correctly. The results are as follows:

The Ember property is passed

Ember also allows you to specify as many parameters as you want, but the way component classes get parameters requires minor modifications. For example, here's an example:

The template that calls the component

  1. !-- apptemplatespassing-properties-to-component.hbs --
  2. {{#each model as item}}
  3. !-- 传递到组件blog-post第一个参数为数据的title值,第二个为body值 --
  4. {{passing-properties-to-component item.title item.body 'third value' 'fourth value'}}
  5. {{each}}

A component class that specifies the name of a parameter, and the way the parameters are .js the Chapter on Calculating Properties in the Getting Started Guide III.

  1. appcomponentspadding-properties-to-component.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. 指定位置参数为参数数组
  5. positionalParams 'params',
  6. title Ember.computed('params.[]', function() {
  7. return this.get('params')[0]; 获取第一个参数
  8. }),
  9. body Ember.computed('params.[]', function() {
  10. return this.get('params')[1]; 获取第二个参数
  11. }),
  12. third Ember.computed('params.[]', function() {
  13. return this.get('params')[2]; 获取第三个参数
  14. }),
  15. fourth Ember.computed('params.[]', function() {
  16. return this.get('params')[3]; 获取第四个参数
  17. })
  18. });

Here's how the component gets the passed parameters.

  1. !-- apptemplatescomponentspassing-properties-to-component.hbs --
  2. article
  3. h1{{title}}h1
  4. p{{body}}p
  5. pthird {{third}}p
  6. pfourth {{fourth}}p
  7. article

The results are as follows:

The Ember property is passed

All the contents passed to this component parameter are covered. O n the whole, it's not that difficult. Parameters in Ember are passed and obtained in much the same way, such as link-to assistants, action assistants.

The full code of the br blog post is placed in Github (the blog post has been modified several times, and the code on the blog may be different from the github code, but the impact is small!). I f you think the blog post is a bit useful to you, please give me a star on the star project. Yours is definitely the biggest motivation for me!!