Skip to content

Instantly share code, notes, and snippets.

@amk221
Last active May 27, 2025 10:54
Show Gist options
  • Save amk221/763ace6c9ad0928e35f3ce2131fdbcf9 to your computer and use it in GitHub Desktop.
Save amk221/763ace6c9ad0928e35f3ce2131fdbcf9 to your computer and use it in GitHub Desktop.
inverse rels
import RESTAdapter from 'ember-data/adapters/rest';
export default class SubscriptionAdapter extends RESTAdapter {}
import RESTAdapter from 'ember-data/adapters/rest';
export default class TenantAdapter extends RESTAdapter {}
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);
}
import Model from 'ember-data/model';
import { belongsTo } from 'ember-data/relationships';
export default class extends Model {
@belongsTo('tenant', {
async: false,
inverse: 'subscription'
}) tenant;
}
import Model from 'ember-data/model';
import { belongsTo } from 'ember-data/relationships';
export default class extends Model {
@belongsTo('subscription', {
async: false,
inverse: 'tenant'
}) subscription;
}
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);
}
import RESTSerializer from 'ember-data/serializers/rest';
export default class SubscriptionSerializer extends RESTSerializer {}
import RESTSerializer from 'ember-data/serializers/rest';
export default class TenantSerializer extends RESTSerializer {}
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
small {
color: gray
}
<p>
<b>Note:</b>
Play with <code>inverse</code> in the models to understand how this works
</p>
<hr />
<dl>
<dt>
Tenant 1 ID:
</dt>
<dd>
{{this.tenant.id}}
</dd>
<dt>
Tenant 1's subscription ID
<small>(aka the 'current' subscription)</small>
</dt>
<dd>
{{this.tenant.subscription.id}}
</dd>
<dt>
Tenant 1's subscription tenant ID
<small>(aka the 'current' subscription's tenant)</small>
</dt>
<dd>
{{this.tenant.subscription.tenant.id}}
</dd>
<dt>
Subscription 1 ID
(aka the original subscription)
</dt>
<dd>
{{this.subscription.id}}
</dd>
</dl>
<br><br>
<button type="button" {{on "click" this.push}}>
Push new subscription
</button>
<br><br>
All tenants ID:<br>
<ul>
{{#each this.tenants as |tenant|}}
<li>{{tenant.id}}</li>
{{/each}}
</ul>
<br><br>
All subscriptions ID:<br>
<ul>
{{#each this.subscriptions as |subscription|}}
<li>{{subscription.id}}</li>
{{/each}}
</ul>
{
"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