Last active
March 29, 2018 13:32
-
-
Save CezaryH/402e5bb073b4f01411556b345b32aba2 to your computer and use it in GitHub Desktop.
model
This file contains hidden or 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 DS from 'ember-data'; | |
| import $ from 'jquery'; | |
| export default DS.RESTAdapter.extend({ | |
| namespace: 'api', | |
| host: 'https://swapi.co', | |
| _queryaaaaa(store, type, query) { | |
| console.log(query); | |
| return new Ember.RSVP.Promise(function(resolve, reject) { | |
| $.getJSON(`https://swapi.co/api/people`, query).then(function(data) { | |
| resolve({ | |
| people: data.results.map((p,i) => {p.id = i; return p;}) | |
| }); | |
| }, function(jqXHR) { | |
| reject(jqXHR); | |
| }); | |
| }); | |
| } | |
| }); |
This file contains hidden or 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({ | |
| appName: 'Ember Twiddle' | |
| }); |
This file contains hidden or 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({ | |
| "name": attr('string') | |
| }); |
This file contains hidden or 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('people'); | |
| }); | |
| export default Router; |
This file contains hidden or 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'; | |
| const { RSVP, Mixin, get } = Ember; | |
| export default Ember.Route.extend({ | |
| page: 1, | |
| unloadAndQuery(modelName, params) { | |
| return new RSVP.Promise((resolve, reject) => { | |
| get(this, 'store').unloadAll(modelName); | |
| get(this, 'store') | |
| .query(modelName, params) | |
| .then(resolve, reject); | |
| }); | |
| }, | |
| model() { | |
| return this.unloadAndQuery('person', { page: this.get('page') }); | |
| }, | |
| afterModel() { | |
| setInterval(() => { | |
| this.set('page', this.get('page') === 1 ? 2 : 1) | |
| this.get('store').query('person', { page: this.get('page') }); | |
| }, 5000) | |
| } | |
| }); |
This file contains hidden or 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.JSONAPISerializer.extend({ | |
| normalizeResponse(store, primaryModelClass, payload, id, requestType) { | |
| return { | |
| data: payload.results.map( (p,i) => { | |
| return { | |
| "attributes": { | |
| "name": p.name | |
| }, | |
| "id": p.name, | |
| "type": "person" | |
| } | |
| }) | |
| }; | |
| } | |
| }); |
This file contains hidden or 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.13.0", | |
| "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