Skip to content

Instantly share code, notes, and snippets.

@acorncom
Forked from feanor07/controllers.application.js
Last active April 26, 2017 16:52
Show Gist options
  • Save acorncom/211e91357ce01bca31c745690a8f182c to your computer and use it in GitHub Desktop.
Save acorncom/211e91357ce01bca31c745690a8f182c to your computer and use it in GitHub Desktop.
so#43547516
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('simple-ajax-route');
this.route('test-promise-route');
this.route('rsvp-promise-route');
this.route('simple-timeout-route');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return {title: 'Initial Title'};
},
actions: {
doAjaxCall() {
new Ember.RSVP.Promise((resolve)=> {
Ember.run.later(this, ()=> {
this.controllerFor(this.get('routeName')).set('model.title', 'modified');
resolve(1);
}, 1000);
});
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return {title: 'Initial Title'};
},
actions: {
doAjaxCall() {
let requestParams = {url: `https://jsonplaceholder.typicode.com/photos/1`,
type: 'GET', contentType: 'application/json', dataType:'json'};
Ember.$.ajax(requestParams).then((json)=>{
Ember.run(()=>{
this.controllerFor(this.get('routeName')).set('model.title', json.title);
});
});
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return {title: 'Initial Title'};
},
actions: {
doAjaxCall() {
Ember.run.later(this, ()=> {
this.controllerFor(this.get('routeName')).set('model.title', 'modified');
}, 3000);
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return {title: 'Initial Title'};
},
actions: {
doAjaxCall() {
new Ember.Test.Promise((resolve)=> {
window.setTimeout(()=> {
Ember.run(()=> {
this.controllerFor(this.get('routeName')).set('model.title', 'modified');
resolve(1);
});
}, 1000);
});
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<br>
{{outlet}}
<br>
<br>
<div class="title">
Title is : {{model.title}}
</div>
<br>
<button onclick={{route-action 'doAjaxCall'}}>Do Ajax Call</button>
<div class="title">
Title is : {{model.title}}
</div>
<br>
<button onclick={{route-action 'doAjaxCall'}}>Do Ajax Call</button>
<div class="title">
Title is : {{model.title}}
</div>
<br>
<button onclick={{route-action 'doAjaxCall'}}>Do Ajax Call</button>
<div class="title">
Title is : {{model.title}}
</div>
<br>
<button onclick={{route-action 'doAjaxCall'}}>Do Ajax Call</button>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('rsvp-promise-route: andThen works with rsvp promise');
test('visiting /rsvp-promise-route', function(assert) {
visit('/rsvp-promise-route');
andThen(function() {
assert.equal(currentURL(), '/rsvp-promise-route');
assert.equal(find('.title').text().trim(), 'Title is : Initial Title');
});
click('button');
andThen(function() {
assert.equal(find('.title').text().trim(), 'Title is : modified');
});
});
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('simple-ajax-route: andThen simply works for ajax call');
test('visiting /simple-ajax-route', function(assert) {
visit('/simple-ajax-route');
andThen(function() {
assert.equal(currentURL(), '/simple-ajax-route');
assert.equal(find('.title').text().trim(), 'Title is : Initial Title');
});
click('button');
andThen(function() {
assert.equal(find('.title').text().trim(), 'Title is : accusamus beatae ad facilis cum similique qui sunt');
});
});
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('simple-timeout-route: andThen works for timeouting routes');
test('visiting /simple-timeout-route', function(assert) {
visit('/simple-timeout-route');
andThen(function() {
assert.equal(currentURL(), '/simple-timeout-route');
assert.equal(find('.title').text().trim(), 'Title is : Initial Title');
});
click('button');
andThen(function() {
assert.equal(find('.title').text().trim(), 'Title is : modified');
});
});
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('test-promise-route: andThen works for test promise');
test('visiting /test-promise-route', function(assert) {
visit('/test-promise-route');
andThen(function() {
assert.equal(currentURL(), '/test-promise-route');
assert.equal(find('.title').text().trim(), 'Title is : Initial Title');
});
click('button');
andThen(function() {
assert.equal(find('.title').text().trim(), 'Title is : modified');
});
});
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-route-action-helper": "2.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment