简体   繁体   English

Ember Octane 升级如何将值从组件传递到控制器

[英]Ember Octane Upgrade How to pass values from component to controller

Upgrade from Ember <3.15 to >=3.15 .从 Ember <3.15升级到>=3.15 How do I pass form values from a controller into a component?如何将表单值从控制器传递到组件?

I cannot begin to explain the number of diagnostic combinations attempted and their corresponding errors received.我无法解释尝试的诊断组合的数量及其收到的相应错误。 So, I figure it best to ask how it should be done correctly?所以,我想最好问问它应该如何正确完成? Is Glimmer involved? Glimmer 是否参与其中?

A simple example: pass a change password from old password to both a new and confirm password via a component to a controller.一个简单的例子:通过一个改变口令old密码两者newconfirm经由到控制器的组件的密码。 In the Component , I keep getting onsubmit() is not a function error.Component 中,我不断收到onsubmit() is not a function错误。

Code example:代码示例:

User Input Form用户输入表格

ChangePasswordForm.hbs更改密码表格.hbs

<div class="middle-box text-center loginscreen animated fadeInDown">
    <div>
        <h3>Change Password</h3>
        <form class="m-t" role="form" {{on "submit" this.changePassword}}>
            {{#each errors as |error|}}
                <div class="error-alert">{{error.detail}}</div>
            {{/each}}
            <div class="form-group">
            {{input type="password" class="form-control" placeholder="Old Password" value=oldPassword required="true"}}
            </div>
            <div class="form-group">
                {{input type="password" class="form-control" placeholder="New Password" value=newPassword required="true"}}
            </div>
            <div class="form-group">
                {{input type="password" class="form-control" placeholder="Confirm Password" value=confirmPassword required="true"}}
            </div>
            <div>
                <button type="submit" class="btn btn-primary block full-width m-b">Submit</button>
            </div>
        </form>
    </div>
</div>

Template Component模板组件

ChangePassword.hbs更改密码.hbs

<Clients::ChangePasswordForm @chgpwd={{this.model}} {{on "submit" this.changePassword}} @errors={{this.errors}} />

Component成分

ChangePasswordForm.js更改密码表单.js

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class ChangePasswordForm extends Component {

    @tracked oldPassword;
    @tracked newPassword;
    @tracked confirmPassword;
    @tracked errors = [];

    @action
    changePassword(ev) {

        // Prevent the form's default action.
        ev.preventDefault();

        this.oldPassword = ev.oldPassword;
        this.newPassword = ev.newPassword;
        this.confirmPassword = ev.confirmPassword;

        // Call the form's onsubmit method and pass in the component's values.

        this.onsubmit({
            oldPassword: this.oldPassword,
            newPassword: this.newPassword,
            confirmPassword: this.confirmPassword
        });
    }
}

Controller控制器

ChangePassword.js更改密码.js

import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';

export default class ChangePassword extends Controller {

    @service ajax 
    @service session

    @action
    changePassword(attrs) { 

        if(attrs.newPassword == attrs.oldPassword)
        {
            this.set('errors', [{
                detail: "The old password and new password are the same.  The password was not changed.",
                status: 1003,
                title: 'Change Password Failed'
            }]);
        }
        else if(attrs.newPassword != attrs.confirmPassword)
        {
            this.set('errors', [{
                detail: "The new password and confirm password must be the same value.  The password was not changed.",
                status: 1003,
                title: 'Change Password Failed'
            }]);
        }
        else
        {
            let token = this.get('session.data.authenticated.token');

            this.ajax.request(this.store.adapterFor('application').get('host') + "/clients/change-password", {
                method: 'POST',
                data: JSON.stringify({ 
                    data: {
                        attributes: {
                            "old-password" : attrs.oldPassword,
                            "new-password" : attrs.newPassword,
                            "confirm-password" : attrs.confirmPassword
                        },
                        type: 'change-passwords'
                    }
                }),
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/vnd.api+json',
                    'Accept': 'application/vnd.api+json'
                }
            })
            .then(() => {

                // Transistion to the change-password-success route.
                this.transitionToRoute('clients.change-password-success');
            })
            .catch((ex) => {

                // Set the errors property to the errors held in the ex.payload.errors.  This will allow the errors to be shown in the UI.
                this.set('errors', ex.payload.errors);
            });
        }
    }
}

Model模型

ChangePassword.js更改密码.js

import Route from '@ember/routing/route';
import AbcAuthenticatedRouteMixin from '../../mixins/abc-authenticated-route-mixin';

export default Route.extend(AbcAuthenticatedRouteMixin, {
//export default class ChangePasswordRoute extends Route(AbcAuthenticatedRouteMixin, {

    model() {

        return {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
        };
    },
})

There is no onsubmit method in @glimmer/component , so you cannot call this.onsubmit inside an action in the component. @glimmer/component this.onsubmit @glimmer/component没有onsubmit方法,因此您不能在组件的操作中调用this.onsubmit

First, you need to pass the action created in your controller to your component.首先,您需要将在控制器中创建的操作传递给您的组件。 This can be done like this:这可以像这样完成:

<ChangePasswordForm @chgpwd={{this.model}} @changePassword={{action 'changePassword'}} />

Remember, you cannot pass data up any more in a glimmer component, you need to use an action since everything is one way binding.记住,你不能再在一个 glimmer 组件中传递数据,你需要使用一个动作,因为一切都是一种方式绑定。

Second, you need to call this action inside your glimmer component:其次,你需要在你的 glimmer 组件中调用这个动作:

this.args.changePassword({
  oldPassword: this.oldPassword,
  newPassword: this.newPassword,
  confirmPassword: this.confirmPassword
});

I've created an Ember Twiddle for you to show this example working.我已经为您创建了一个Ember Twiddle来展示这个示例的工作。

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

相关问题 Ember Octane 升级:如何处理 eslint 错误无操作 - Ember Octane upgrade: how to handle eslint error no-action 如何在子组件 ember octane 中监听父组件值的变化? - How to listen parent component value change in child component ember octane? Ember Octane:对控制器进行单元测试异步操作 - Ember Octane: Unit testing async action on controller 如何将值从Visualforce组件中的脚本传递到其控制器 - How to pass values from Script in visualforce component to its controller 如何将控制器数据传递到组件中并在ember js中备份 - how to pass controller data into component and back up in ember js 如何通过Ember.js中的操作将多个值从视图传递到控制器? - How to pass multiple values from view to controller trough an action in Ember.js? 如何将参数传递给余烬组件 - how to pass parameters to ember component 如何从余烬控制器或组件观察车把模板中的多个状态 - How to observe multiple states in a handlebar template from an ember controller or component 如何将数据从控制器中的动作传递到 Ember 中的把手模板 - How to pass data from action in controller, to the handlebar template in Ember 如何仅将模型 [0] 从 Ember 中的模板传递给组件? - How to pass only model[0] to a component from the template in Ember?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM