Skip to content

Instantly share code, notes, and snippets.

@finferflu
Created August 14, 2017 19:53
Show Gist options
  • Save finferflu/d9de007c02765d2f815e5a75c33d37ec to your computer and use it in GitHub Desktop.
Save finferflu/d9de007c02765d2f815e5a75c33d37ec to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Component.extend({
title: '',
init () {
this._super(...arguments)
let type = this.get('type')
this.set('title', type.charAt(0).toUpperCase() + type.slice(1))
}
});
import Ember from 'ember';
export default Ember.Component.extend({
headings: [],
keys: [],
modelNames: {},
modelIDs: {},
init () {
this._super(...arguments)
this.set('headings', [])
this.set('keys', [])
this.set('modelNames', {})
this.set('modelIDs', {})
console.log('DID RECEIVE ATTRS')
console.log(this.get('exclude'))
const listingTypes = {
device: {
id: ['ID', 'device'],
locationName: ['Location', 'location']
},
location: {
id: ['ID', 'location'],
name: ['Name', 'location'],
city: ['City']
}
}
const listing = listingTypes[this.get('type')]
let keys = Object.keys(listing)
if (this.get('exclude')) {
const excludes = this.get('exclude').split(',').map((e) => e.trim())
keys = Object.keys(listing)
.filter(e => { return excludes.indexOf(e) < 0 })
}
let modelNames = {}
let modelIDs = {}
keys.forEach(k => {
let id = listing[k][1] + '.id'
if (this.get('type') === listing[k][1]) {
id = 'id'
}
modelNames[k] = listing[k][1]
modelIDs[k] = id
})
this.set('modelNames', modelNames)
this.set('modelIDs', modelIDs)
const headings = keys.map(key => { return listing[key][0] })
this.set('headings', headings)
this.set('keys', keys)
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import Ember from 'ember';
export default Model.extend({
name: DS.attr(),
location: DS.belongsTo(),
locationName: Ember.computed.alias('location.name')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: DS.attr(),
city: DS.attr(),
devices: DS.hasMany('device', {async: true})
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('locations', function() {
this.route('show', {path: '/:location_id'}, function() {
this.route('devices');
});
});
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return [{
id: 1,
name: 'Location 0',
city: 'San Francisco'
},
{
id: 2,
name: 'Location 1',
city: 'London'
}]
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return [{
id: 1,
name: 'Device 0',
location: 1
},
{
id: 2,
name: 'Device 1',
location: 1
}]
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return {
id: 1,
name: 'Location 0',
city: 'San Francisco'
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<h1>{{title}} List</h1>
{{listing-table model=model type=type}}
<table class="ui striped celled table">
<thead>
<tr>
{{#each headings as |heading|}}
<th>{{heading}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each model as |mod|}}
<tr class='{{type}}-listing'>
{{#each keys as |key|}}
<td>
{{#if (get modelNames key)}}
{{#link-to (concat (get modelNames key) 's.show') (get mod (get modelIDs key))}}
{{get mod key}}
{{/link-to}}
{{else}}
{{get mod key}}
{{/if}}
</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
{{index-view model=model type='location'}}
{{listing-table model=model.devices type='device' exclude='locationName'}}
<h1>{{model.name}}</h1>
<h3>{{model.city}}</h3>
{{#link-to 'locations.show.devices' model.id}}Show devices{{/link-to}}
{{outlet}}
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('TODO: put something here');
test('visiting /locations', function(assert) {
visit('/locations');
click('a:contains("Location 0")')
click('a:contains("Show devices")')
andThen(function() {
assert.equal(find('th').text().trim(), 'ID', 'should only contain ID and no Location')
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
const { RSVP: { Promise } } = Ember;
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => destroyApp(this.application));
}
});
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"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-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment