Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Forked from jrr/controllers.application.js
Created January 27, 2017 16:30
Show Gist options
  • Save samselikoff/c629535ba187e43f074dfb2e6c92b3d5 to your computer and use it in GitHub Desktop.
Save samselikoff/c629535ba187e43f074dfb2e6c92b3d5 to your computer and use it in GitHub Desktop.
Mirage embed?
import Ember from 'ember';
export default Ember.Controller.extend({
_observeModel: Ember.observer('model', function(){
//console.log('model is',this.get('model'));
})
});
import Collection from 'ember-cli-mirage/orm/collection';
export default function() {
window.server = this;
this.get('/foo', function(schema, request){
var post = schema.posts.all().models[0];
//console.log('post:',post);
//console.log('serialized:',this.serialize(post));
return post;
});
};
import { Factory, faker } from 'ember-cli-mirage';
export default Factory.extend({
name() {
return `${faker.name.firstName()} ${faker.name.lastName()}`;
}
});
import { Factory, faker } from 'ember-cli-mirage';
export default Factory.extend({
title: 'Once upon a time',
afterCreate(post, server){
console.log('post factory afterCreate');
post.update({
author: server.create('author')
});
}
});
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
});
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
author: belongsTo()
});
export default function(server) {
server.create('post');
console.log('created post',server.schema.posts.first());
}
import { RestSerializer } from 'ember-cli-mirage';
export default RestSerializer.extend({
root: false,
embed: true,
serialize(object) {
// This is how to call super, as Mirage borrows [Backbone's implementation of extend](http://backbonejs.org/#Model-extend)
let json = RestSerializer.prototype.serialize.apply(this, arguments);
console.log('application serializer, object:', object);
console.log('serialized:',json);
return json;
}
});
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
include: ['author']
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return $.get('/foo');
}
});
body {
margin: 0;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
header {
background: #2e4068;
color: white;
padding: 14px 20px;
}
h1 {
font-size: 12px;
font-weight: bold;
text-shadow: none;
margin: 0 0 2px;
text-transform: uppercase;
opacity: 0.5;
}
h2 {
font-size: 16px;
margin: 0;
}
p {
font-weight: normal;
}
.pa4 {
padding: 20px;
}
.ph4 {
padding-left: 20px;
padding-right: 20px;
}
hr {
border-top: none;
border-bottom: 1px solid #ddd;
}
.panel {
background: whitesmoke;
}
.panel__toggle {
display: block;
text-decoration: none;
padding: 8px;
color: #777;
}
.panel__body {
padding: 8px;
}
<header>
<h1>Mirage</h1>
</header>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Acceptance | Users');
test('I can see the users', function(assert) {
server.create('user', { name: 'Link' });
//visit('/');
andThen(function() {
assert.ok(true);
//assert.ok(find(':contains(Welcome Link!)').length);
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
afterEach() {
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
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';
export default function startApp(attrs) {
let application;
let attributes = Ember.merge({rootElement: "#test-root"}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;
Ember.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.10.7",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.0",
"ember-template-compiler": "2.10.0"
},
"addons": {
"ember-cli-mirage": "0.2.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment