This is a very trivial example of using mirage with Ember Twiddle.
Last active
October 20, 2017 15:01
-
-
Save allthesignals/56887484a75220658fc047095df95d3c to your computer and use it in GitHub Desktop.
Ember Twiddle Demo: Mirage with Ember Twiddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.RESTAdapter.extend({ | |
namespace: '/api' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.JSONAPIAdapter.extend({ | |
namespace: '/api' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
actions: { | |
addBookmark(bookmark) { | |
this.store.createRecord('bookmark', { bookmark }).save(); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
actions: { | |
addBookmark(bookmark) { | |
this.store.createRecord('bookmark', { bookmark }).save(); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Collection from 'ember-cli-mirage/orm/collection'; | |
export default function() { | |
window.server = this; | |
this.namespace = '/api'; | |
this.get('/novels', function (schema, request) { | |
var novels ={ | |
novels: [ | |
{ id: 1, name: 'Neuromancer' }, | |
{ id: 2, name: 'The Hours' }, | |
{ id: 3, name: 'Tales of the City' } | |
] | |
}; | |
return novels; | |
}); | |
this.get('/bookmarks', function (schema, request) { | |
var data ={ | |
data: [ | |
{ | |
"id":1, | |
"attributes":{ | |
"address":null | |
}, | |
"relationships":{ | |
"bookmark":{ | |
"data":{ | |
"type":"novel", | |
"id":"1" | |
} | |
} | |
}, | |
"type":"bookmarks" | |
}, | |
] | |
}; | |
return data; | |
}); | |
this.post('/bookmarks', function(schema, request) { | |
const response = JSON.parse(request.requestBody); | |
response.data.id = 2; | |
return JSON.parse(JSON.stringify(response)); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
export default Model.extend({ | |
bookmark: belongsTo('bookmark', { inverse: 'bookmark' }) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Bookmarkable from './bookmark'; | |
export default Bookmarkable.extend({ | |
name: DS.attr('string') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('lot', { path: 'lots/:id' }); | |
}); | |
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model() { | |
return this.store.findAll('novel').then((novels) => { | |
// I always need this line: | |
this.store.findAll('bookmark'); | |
return novels; | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model(params) { | |
return this.store.findRecord('lot', params.id); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.11.3", | |
"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.11.0", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.11.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