gpt4 book ai didi

javascript - Ember Octane 升级如何将值从组件传递到 Controller

转载 作者:行者123 更新时间:2023-12-03 16:21:54 26 4
gpt4 key购买 nike

从 Ember 升级 <3.15>=3.15 .如何将表单值从 Controller 传递到组件?

我无法开始解释尝试的诊断组合的数量以及收到的相应错误。所以,我想最好问问应该如何正确完成?微光参与了吗?

一个简单的例子:传递更改密码来自 old密码 两者都是 newconfirm通过组件到 Controller 的密码。在 组件 ,我不断收到onsubmit() is not a function错误。

代码示例:

用户输入表单

ChangePasswordForm.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>

模板组件

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

零件

ChangePasswordForm.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
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);
});
}
}
}

模型

ChangePassword.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: ''
};
},
})

最佳答案

没有onsubmit @glimmer/component 中的方法,所以你不能调用this.onsubmit在组件的一个 Action 中。
首先,您需要将在 Controller 中创建的操作传递给您的组件。这可以这样做:<ChangePasswordForm @chgpwd={{this.model}} @changePassword={{action 'changePassword'}} />请记住,您不能再在 glimmer 组件中传递数据,您需要使用操作,因为一切都是单向绑定(bind)。
其次,您需要在 glimmer 组件中调用此操作:

this.args.changePassword({
oldPassword: this.oldPassword,
newPassword: this.newPassword,
confirmPassword: this.confirmPassword
});
我创建了一个 Ember Twiddle让你展示这个例子的工作。

关于javascript - Ember Octane 升级如何将值从组件传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60700250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com