Skip to content

Instantly share code, notes, and snippets.

@olyckne
Last active February 24, 2022 11:38
Show Gist options
  • Save olyckne/89e1a8168304360b322981218ee218ff to your computer and use it in GitHub Desktop.
Save olyckne/89e1a8168304360b322981218ee218ff to your computer and use it in GitHub Desktop.
input {{on}}
import Controller from '@ember/controller';
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";
export default class ApplicationController extends Controller {
name = "";
@tracked nameSetInValidateName = "";
@tracked error = "Name must be at least 3 charaters";
@action
validateName(event) {
this.nameSetInValidateName = this.name
this.error = "";
if (this.name.length < 3) {
this.error = "Name must be at least 3 charaters";
}
}
}
<p>Using this way "nameSetInValidName" will be one character after "name"</p>
<code>&lt;Input @value=\{{this.name}} \{{on "input" this.validateName}} /&gt;</code><br />
<Input
@value={{this.name}}
{{on "input" this.validateName}}
/>
<hr />
<br />
<p>Using this was they will both be in sync</p>
<code>&lt;Input @value=\{{this.name}} @input=\{{this.validateName}} /&gt;</code><br />
<Input
@value={{this.name}}
@input={{this.validateName}}
/>
<hr />
<br />
Output:
<output>
<p>name: {{this.name}}</p>
<p>nameSetInValidateName: {{this.nameSetInValidateName}}</p>
</output>
{{#if this.error}}
<p style="color: red;">
{{this.error}} <br />
Length is {{this.name.length}}
</p>
{{/if}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment