Skip to content

Instantly share code, notes, and snippets.

@nightire
Created November 6, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nightire/b52ef7dfecbd6a08c86d to your computer and use it in GitHub Desktop.
Save nightire/b52ef7dfecbd6a08c86d to your computer and use it in GitHub Desktop.
Unable to use mockjax with ember data
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Route.extend({
init() {
this._super(...arguments)
$.mockjax({
url: '/parents/1',
responseText: {
data: {
type: 'parent',
id: '1',
relationships: {
child: {
data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'].map(child => ({ type: 'child', id: child }))
}
}
}
}
});
$.mockjax({
url: /^\/children\/(\d+)$/i,
urlParams: ["childId"],
response: function(settings) {
this.responseText = {
data: {
type: 'child',
id: settings.urlParams.childId,
attributes: {
parent: '1'
}
}
}
}
});
},
model() {
return this.store.find('parent', '1')
}
});
<h1>Welcome to {{appName}}</h1>
<p>Odd children count: {{model.oddChildren.length}}</p>
<p>Odd children ids:</p>
<ul>
{{#each model.oddChildren key="id" as |child|}}
<li>{{child.id}}</li>
{{/each}}
</ul>
import DS from 'ember-data';
export default DS.Model.extend({
parent: DS.belongsTo('parent', { async: true })
});
import DS from 'ember-data';
export default DS.Model.extend({
children: DS.hasMany('child', { async: true }),
oddChildren: Ember.computed('children.@each.id', function() {
return DS.PromiseArray.create({
promise: this.get('children').then(function(children) {
return children.filter(function(child) {
return child.get('id') % 2 !== 0
});
})
});
})
});
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.1.0",
"ember-data": "2.1.0",
"ember-template-compiler": "2.1.0",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment