簡體   English   中英

Ember Octane 升級如何將值從組件傳遞到控制器

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

從 Ember <3.15升級到>=3.15 如何將表單值從控制器傳遞到組件?

我無法解釋嘗試的診斷組合的數量及其收到的相應錯誤。 所以,我想最好問問它應該如何正確完成? Glimmer 是否參與其中?

一個簡單的例子:通過一個改變口令old密碼兩者newconfirm經由到控制器的組件的密碼。 Component 中,我不斷收到onsubmit() is not a function錯誤。

代碼示例:

用戶輸入表格

更改密碼表格.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>

模板組件

更改密碼.hbs

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

成分

更改密碼表單.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
        });
    }
}

控制器

更改密碼.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);
            });
        }
    }
}

模型

更改密碼.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: ''
        };
    },
})

@glimmer/component this.onsubmit @glimmer/component沒有onsubmit方法,因此您不能在組件的操作中調用this.onsubmit

首先,您需要將在控制器中創建的操作傳遞給您的組件。 這可以像這樣完成:

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

記住,你不能再在一個 glimmer 組件中傳遞數據,你需要使用一個動作,因為一切都是一種方式綁定。

其次,你需要在你的 glimmer 組件中調用這個動作:

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

我已經為您創建了一個Ember Twiddle來展示這個示例的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM