Skip to content

Instantly share code, notes, and snippets.

@chancancode
Last active June 16, 2017 18:51
Show Gist options
  • Save chancancode/4adde6ca35c70ee3ae57e8a10cf55176 to your computer and use it in GitHub Desktop.
Save chancancode/4adde6ca35c70ee3ae57e8a10cf55176 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
query: '',
actions: {
doSearch(query) {
this.replaceRoute('search', { queryParams: { query } });
}
}
});
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
const { computed, inject: { service } } = Ember;
export default Ember.Controller.extend({
queryParams: ['query'],
query: '',
ajax: service(),
search: task(function * (type, query) {
console.log(`Searching ${type} for ${query}...`);
yield timeout(Math.random() * 5000);
let result = yield this.get('ajax').request(`https://api.github.com/search/${type}?q=${query}`);
console.log(`Done searching ${type} for ${query}.`);
return result.items;
}),
userSearch: computed('query', function() {
let query = this.get('query');
return this.get('search').perform('users', query);
}),
repoSearch: computed('query', function (){
let query = this.get('query');
return this.get('search').perform('repositories', query);
})
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('search');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
queryParams: {
query: { refreshModel: true }
},
model(params) {
return params.query;
},
activate() {
let query = this.modelFor('search');
this.controllerFor('application').set('query', query);
},
deactivate() {
this.controllerFor('application').set('query', '');
},
});
body {
margin: 62px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
header {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 50px;
background: #efefef;
padding: 0;
margin: 0;
}
header > * {
display: inline-block;
height: 50px;
line-height: 50px;
font-size: 24px;
padding: 0 16px;
margin: 0;
}
header > .search {
float: right;
}
<header>
<h1>Welcome to {{appName}}</h1>
<p class="search">Search: {{input type="search" enter="doSearch" value=query placeholder="Enter to search..."}}</p>
</header>
{{outlet}}
<h2>Such app</h2>
<h3>Something to try...</h3>
<ul>
<li>{{link-to "machty" "search" (query-params query="machty")}}</li>
<li>{{link-to "ember.js" "search" (query-params query="ember.js")}}</li>
<li>{{link-to "ember-concurrency" "search" (query-params query="ember-concurrency")}}</li>
</ul>
<h2>Showing results for {{query}}</h2>
<h3>Users</h3>
{{#if userSearch.isRunning}}
<p>Loading...</p>
{{else if userSearch.value}}
<ul>
{{#each userSearch.value as |user|}}
<li><a href="{{user.html_url}}" target="_blank">{{user.login}}</a></li>
{{/each}}
</ul>
{{else}}
<p>Nothing to see here.</p>
{{/if}}
<h3>Repositories</h3>
{{#if repoSearch.isRunning}}
<p>Loading...</p>
{{else if repoSearch.value}}
<ul>
{{#each repoSearch.value as |repo|}}
<li><a href="{{repo.html_url}}" target="_blank">{{repo.full_name}}</a></li>
{{/each}}
</ul>
{{else}}
<p>Nothing to see here.</p>
{{/if}}
<p>{{link-to "Go back" "index"}}</p>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-ajax": "*",
"ember-concurrency": "*",
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment