Skip to content

Instantly share code, notes, and snippets.

@GCheung55
Created April 11, 2018 04:27
Show Gist options
  • Save GCheung55/7d8b5262a2c0891713127c941b3311f1 to your computer and use it in GitHub Desktop.
Save GCheung55/7d8b5262a2c0891713127c941b3311f1 to your computer and use it in GitHub Desktop.
Example: pushing records into ember-data store
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
const { getOwner } = Ember;
// Here is an example of using an instance-initializer for inserting records into the store.
// It's basically doing the same thing as in routes/application.js
// Not that the instance-initializer executes first, so the records that are found can be out of order.
export function initialize(app) {
const store = app.lookup('service:store');
store.pushPayload('my-model', {
data: {
type: 'my-model',
id: 3,
attributes: {
awesome: true
}
}
})
}
export default {
name: 'insert-records-example',
initialize
};
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
// isAwesome is going to be mapped from 'awesome', which is mapped in the serializer
// I'm just demonstrating that the serializer is used to normalize the pushed payload.
isAwesome: attr('boolean')
});
import Ember from 'ember';
// Not using @ember namespace because Twiddle doesn't support it... I think.
const { get } = Ember;
export default Ember.Route.extend({
model() {
const store = get(this, 'store');
/*
* This uses the `my-model` serializer to normalize the records.
* `store.pushPayload` will execute the serializer's normalize method, but not the other
* normalizeResponse or normalize###Response methods.
*
* If you need to use the normalizeResponse or normalize###Response methods for whatever reason,
* the serialize will need to be looked up. Fortunately that's pretty easy with `store.serializerFor` method.
*/
store.pushPayload('my-model', {
data: [
{ type: 'my-model', id: 1, attributes: { awesome: true }},
{ type: 'my-model', id: 2, attributes: { awesome: false }}
]
});
return store.peekAll('my-model');
}
});
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
attrs: {
// <what-we-want-the-key-to-be>: <what-the-response-contains>
isAwesome: 'awesome'
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{#each model key="id" as | record |}}
<p>
Model {{record.id}} is {{unless record.isAwesome "not"}} awesome!
</p>
{{/each}}
{
"version": "0.13.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment