Skip to content

Instantly share code, notes, and snippets.

@HenryVonfire
Forked from mike-north/components.my-component.js
Last active September 5, 2018 07:34
Show Gist options
  • Save HenryVonfire/8017fec1a6c2991811b2007c5edf2bcd to your computer and use it in GitHub Desktop.
Save HenryVonfire/8017fec1a6c2991811b2007c5edf2bcd to your computer and use it in GitHub Desktop.
component-data-provider
import Ember from 'ember';
// it makes the issue of async really simple, in that you just care about the array {{my-component}} yields out
// it may at some times be empty, it may have some stuff later -- you're just binding to it. No promises for you to worry about
// you could do the same thing with the API call currently in your controller, and then break out code that consumes that data into other components.
// to make this even fancier, you could add Ember.run.debounce to avoid race conditions
export default Ember.Component.extend({
term: '',
init() {
this._super(...arguments);
this.set('results', []);
},
didReceiveAttrs() {
this._super(...arguments);
this.getSearchResults();
},
getSearchResults() {
fetch('https://api.mike.works/api/v1/search?types=course&q=' + this.get('term'))
.then(response => response.json())
.then(responseData => {
this.get('results').setObjects(responseData);
});
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
searchTerm: 'node'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<input type="text" value={{searchTerm}} onInput={{action (mut searchTerm) value='target.value'}} />
{{#my-component term=searchTerm as |results|}}
<ul>
{{#each results as |result|}}
<li>{{result.name}}</li>
{{/each}}
</ul>
{{/my-component}}
<br>
<br>
{
"version": "0.15.0",
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment