Forked from wuarmin/controllers.application.js
Last active
February 16, 2018 08:53
-
-
Save mhluska/db0bd0068cc0503e732ca06c15a2db9a to your computer and use it in GitHub Desktop.
async_model_loading
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: 'Videos' | |
}); |
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 Mirage from 'ember-cli-mirage'; | |
import Ember from 'ember'; | |
export default function() { | |
this.get('/videos', function(db, request) { | |
let videos = [ | |
{ id: '1', name: 'Die Hard 1', userId: '1' }, | |
{ id: '2', name: 'Die Hard 2', userId: '2' }, | |
{ id: '3', name: 'Die Hard 3', userId: '1' }, | |
{ id: '4', name: 'Die Hard 4', userId: '3' }, | |
{ id: '5', name: 'Die Hard 5', userId: '4' }, | |
{ id: '6', name: "Jack's Bicycle", userId: '4' }, | |
{ id: '7', name: 'Flight of the Phoenix', userId: '4' }, | |
{ id: '8', name: 'The Hunt for Red October', userId: '1' }, | |
// This video intentionally doesn't get loaded for `/videos`. | |
{ id: '9', name: 'Die Hard 6', userId: '4' }, | |
]; | |
if (request.queryParams.user_id) { | |
return { | |
data: videos.filterBy('userId', request.queryParams.user_id).map((video) => { | |
return { | |
type: 'videos', | |
id: video.id, | |
attributes: { name: video.name }, | |
relationships: { "my-user": { data: { type: "users", id: video.userId } } } | |
} | |
}) | |
}; | |
} else { | |
return { | |
data: videos.slice(0, 8).map((video) => { | |
return { | |
type: 'videos', | |
id: video.id, | |
attributes: { name: video.name }, | |
relationships: { "my-user": { data: { type: "users", id: video.userId } } } | |
} | |
}) | |
} | |
} | |
}); | |
this.get('/users/1', function() { | |
return { | |
data: { | |
type: 'users', | |
id: '1', | |
attributes: { | |
name: 'John McTiernan', | |
}, | |
relationships: { | |
videos: { | |
links: { | |
related: "/videos?user_id=1" | |
} | |
} | |
} | |
} | |
}; | |
}); | |
this.get('/users/2', function() { | |
return { | |
data: { | |
type: 'users', | |
id: '2', | |
attributes: { | |
name: 'Renny Harlin', | |
}, | |
relationships: { | |
videos: { | |
links: { | |
related: "/videos?user_id=2" | |
} | |
} | |
} | |
} | |
}; | |
}); | |
this.get('/users/3', function() { | |
return { | |
data: { | |
type: 'users', | |
id: '3', | |
attributes: { | |
name: 'Len Wiseman', | |
}, | |
relationships: { | |
videos: { | |
links: { | |
related: "/videos?user_id=3" | |
} | |
} | |
} | |
} | |
}; | |
}); | |
this.get('/users/4', function() { | |
return { | |
data: { | |
type: 'users', | |
id: '4', | |
attributes: { | |
name: 'John Moore', | |
}, | |
relationships: { | |
videos: { | |
links: { | |
related: "/videos?user_id=4" | |
} | |
} | |
} | |
} | |
}; | |
}); | |
} |
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'), | |
videos: hasMany('video') | |
}); |
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'), | |
myUser: belongsTo('user') | |
}); | |
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('videos', function() {}); | |
this.route('users', function() { | |
this.route('show', { path: '/:user_id' }); | |
}); | |
}); | |
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'; | |
export default Ember.Route.extend({ | |
model(params) { | |
return this.get('store').findRecord('user', params.user_id); | |
} | |
}); |
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.Route.extend({ | |
model() { | |
return this.get('store').query('video', { | |
limit: 8 | |
}); | |
} | |
}); |
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", | |
"ENV": { | |
"ember-cli-mirage": { | |
"enabled": true | |
} | |
}, | |
"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", | |
"ember-cli-mirage": "0.4.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment