-
-
Save runspired/1d395fcf2cfb39227599192cebe8ec5f to your computer and use it in GitHub Desktop.
ember polymorphic sideload
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({ | |
appName: 'Ember Twiddle', | |
init() { | |
this._super(); | |
this.posts = this.get('store').peekAll('post'); | |
this.comments = this.get('store').peekAll('comment'); | |
}, | |
actions: { | |
clear() { | |
let store = this.get('store'); | |
store.unloadAll('post'); | |
store.unloadAll('comment'); | |
this.set('model', null); | |
}, | |
getPost(id) { | |
let store = this.get('store'); | |
this.set('model', store.peekRecord('post', id)); | |
}, | |
loadPosts() { | |
let store = this.get('store'); | |
store.push({ | |
data: { | |
id: 1, | |
type: 'post', | |
attributes: { name: 'Post 1' } | |
} | |
}); | |
}, | |
loadComments() { | |
let store = this.get('store'); | |
store.push({ | |
data: [ | |
{ | |
type: 'comment', | |
id: '1', | |
attributes: { | |
commentableId: 1, | |
commentableType: 'Post' | |
}, | |
relationships: { | |
commentable: { | |
data: { id: '1', type: 'post' } | |
} | |
} | |
}, | |
{ | |
type: 'comment', | |
id: '2', | |
attributes: { | |
commentableId: 1, | |
commentableType: 'Post' | |
}, | |
relationships: { | |
commentable: { | |
data: { id: '1', type: 'post' } | |
} | |
} | |
} | |
] | |
}); | |
} | |
} | |
}); |
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 DS from 'ember-data'; | |
export default Ember.Mixin.create({ | |
comments: DS.hasMany( | |
'comment', | |
{ | |
async: true, | |
inverse: 'commentable' | |
} | |
), | |
}); |
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({ | |
commentableType: attr(), | |
commentableId: attr(), | |
commentable: belongsTo( | |
'commentable', | |
{ | |
inverse: 'comments', | |
polymorphic: true, | |
async: true | |
} | |
), | |
}); |
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 Commentable from 'twiddle/mixins/commentable'; | |
export default Model.extend(Commentable, { | |
name: attr() | |
}); |
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.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.17" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment