Skip to content

Instantly share code, notes, and snippets.

@jacobq
Last active December 17, 2018 22:47
Show Gist options
  • Save jacobq/8f15c1d3f19c48aaab90ac5495cd5a50 to your computer and use it in GitHub Desktop.
Save jacobq/8f15c1d3f19c48aaab90ac5495cd5a50 to your computer and use it in GitHub Desktop.
{{input type="number"}}
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
numberChanged(e) {
const stringValue = e.currentTarget.value;
const numValue = parseFloat(stringValue);
console.log("No, really, this is a number!", numValue, typeof numValue);
this.set('data', numValue);
}
}
});
import Ember from 'ember';
import { computed } from '@ember/object';
export default Ember.Controller.extend({
appName: '{{input type="number"}}',
myNumber: 3,
typeOfMyNumber: computed('myNumber', function() {
return typeof this.myNumber;
})
});
<h1>{{appName}}</h1>
<br>
myNumber: {{my-component data=myNumber}}
is a {{typeOfMyNumber}}
{{outlet}}
<footer>
<p>
<strong>Instructions:</strong>
Click the up/down spinner or enter new values for <code>myNumber</code> and observe its type.
</p>
<p>
<strong>Problem/Question:</strong>
Why isn't <code>myNumber</code> always a number?
</p>
<hr>
{{!--
!!! SPOILER ALERT !!!
Answers / hints may be below here, so don't scroll down unless you want to see them.
... just a little further ...
--}}
<style>
ul {
list-style: none;
}
summary + p {
margin-left: 18px;
}
summary {
margin-bottom: 8px;
}
</style>
<details>
<summary>Hints</summary>
<ul>
<li>
<details>
<summary>Hint #1</summary>
<p>
What happens after the <code>change</code> action fires?
</p>
</details>
</li>
<li>
<details>
<summary>Hint #2</summary>
<p>
Would the same problem happen if we used <code>&lt;input ...&gt;</code> instead of <code>&#123;{input ...}}</code>?
</p>
</details>
</li>
</ul>
</details>
<details>
<summary>Answer</summary>
:P
</details>
<p>
Check out more puzzlers at
<a href="https://github.com/EmberMN/ember-puzzlers/">EmberMN/ember-puzzlers</a>.
</p>
</footer>
{{input
type="number"
value=data
change=(action "numberChanged")
}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment