Skip to content

Instantly share code, notes, and snippets.

@CezaryH
Created December 4, 2018 17:00
Show Gist options
  • Save CezaryH/e2769917896fd88fe2b2b4d5aae731d5 to your computer and use it in GitHub Desktop.
Save CezaryH/e2769917896fd88fe2b2b4d5aae731d5 to your computer and use it in GitHub Desktop.
unload 2
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host: 'https://jsonplaceholder.typicode.com'
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
loadPosts() {
this.store.findAll('post');
this.send('refresh');
},
unloadPosts() {
this.store.unloadAll('post');
this.send('refresh');
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
"userId": attr(''),
"title": attr(''),
"body": attr('')
});
import Ember from 'ember';
export default Ember.Route.extend({
i: 1,
async model() {
this._super(...arguments);
return this.store.peekAll('post');
},
actions: {
refresh() {
this.refresh();
}
}
});
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
i: 0,
normalizeSingleResponse(store, primaryModelClass, payload, id, requestType) {
payload = {
post: payload
};
return this._super(store, primaryModelClass, payload, id, requestType);
},
normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
let max = 2 + this.i;
payload = {
posts: payload.slice(this.i, max)
};
this.i = this.i + 2;
if(this.i === 8) {
this.i = 0;
}
return this._super(store, primaryModelClass, payload, id, requestType);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<button {{action 'unloadPosts'}}>unload</button>
<button {{action 'loadPosts'}}>load</button>
<br>
{{#each model as |m| }}
title:{{m.id}} - {{m.title}} <br>
{{/each}}
<br>
<br>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment