Last active
May 27, 2025 10:54
-
-
Save amk221/763ace6c9ad0928e35f3ce2131fdbcf9 to your computer and use it in GitHub Desktop.
inverse rels
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 RESTAdapter from 'ember-data/adapters/rest'; | |
export default class SubscriptionAdapter extends RESTAdapter {} |
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 RESTAdapter from 'ember-data/adapters/rest'; | |
export default class TenantAdapter extends RESTAdapter {} |
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 Controller from '@ember/controller'; | |
import { action } from '@ember/object'; | |
import { inject } from '@ember/service'; | |
export default class ApplicationController extends Controller { | |
@inject store; | |
@action | |
push() { | |
pushPayload(this.store, 'subscription', { | |
subscription: { | |
id: 2, | |
tenant: 1 | |
}, | |
tenant: { | |
id: 1 | |
} | |
}); | |
} | |
} | |
// An alternative to store.pushPayload() that supports sideloaded records | |
// See: https://api.emberjs.com/ember-data/3.18/classes/MinimumSerializerInterface/methods?anchor=normalize%20%5BOPTIONAL%5D | |
export function pushPayload(store, modelName, rawPayload) { | |
const ModelClass = store.modelFor(modelName); | |
const serializer = store.serializerFor(modelName); | |
const jsonApiPayload = serializer.normalizeResponse(store, ModelClass, rawPayload, null, 'query'); | |
return store.push(jsonApiPayload); | |
} |
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 { belongsTo } from 'ember-data/relationships'; | |
export default class extends Model { | |
@belongsTo('tenant', { | |
async: false, | |
inverse: 'subscription' | |
}) tenant; | |
} |
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 { belongsTo } from 'ember-data/relationships'; | |
export default class extends Model { | |
@belongsTo('subscription', { | |
async: false, | |
inverse: 'tenant' | |
}) subscription; | |
} |
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 Route from '@ember/routing/route'; | |
import { inject } from '@ember/service'; | |
export default class extends Route { | |
@inject store; | |
beforeModel() { | |
pushPayload(this.store, 'tenant', { | |
tenant: { | |
id: 1, | |
subscription: 1 | |
}, | |
subscription: { | |
id: 1 | |
} | |
}); | |
} | |
setupController(controller, model) { | |
super.setupController(...arguments); | |
controller.tenant = this.store.peekRecord('tenant', 1); | |
controller.subscription = this.store.peekRecord('subscription', 1); | |
controller.tenants = this.store.peekAll('tenant'); | |
controller.subscriptions = this.store.peekAll('subscription'); | |
} | |
} | |
// An alternative to store.pushPayload() that supports sideloaded records | |
// See: https://api.emberjs.com/ember-data/3.18/classes/MinimumSerializerInterface/methods?anchor=normalize%20%5BOPTIONAL%5D | |
export function pushPayload(store, modelName, rawPayload) { | |
const ModelClass = store.modelFor(modelName); | |
const serializer = store.serializerFor(modelName); | |
const jsonApiPayload = serializer.normalizeResponse(store, ModelClass, rawPayload, null, 'query'); | |
return store.push(jsonApiPayload); | |
} |
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 RESTSerializer from 'ember-data/serializers/rest'; | |
export default class SubscriptionSerializer extends RESTSerializer {} |
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 RESTSerializer from 'ember-data/serializers/rest'; | |
export default class TenantSerializer extends RESTSerializer {} |
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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
small { | |
color: gray | |
} |
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.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-data": "3.18.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment