Skip to content

Instantly share code, notes, and snippets.

@lifeart
Forked from korneev-andrew/controllers.application\.js
Last active November 18, 2020 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeart/ed259f439203096ac88c0e0aaa3383f1 to your computer and use it in GitHub Desktop.
Save lifeart/ed259f439203096ac88c0e0aaa3383f1 to your computer and use it in GitHub Desktop.
Pushing new meta-attr
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
@service('store') store;
constructor() {
super(...arguments);
this.setup();
}
@action setup() {
this.store.pushPayload({
included: [
{id: '1', type: 'meta-attribute', attributes: { value: 'a', name: 'firstname' } }, {id: '2', type: 'meta-attribute', attributes: { value: 'b', name: 'lastname' } }
],
data: {
id: '1',
type: 'meta-model',
relationships: {
'meta-attributes': {
data: [
{id: '1', type: 'meta-attribute'},
{id: '2', type: 'meta-attribute'},
]
}
}
}
});
this.set('items', this.store.peekAll('meta-model'));
}
@action pushMetaAdditive() {
this.store.createRecord('meta-additive', { parent: this.store.peekAll('meta-attribute').firstObject });
}
@action pushMetaAttribute() {
const model = this.store.peekRecord('meta-model', '1');
this.store.createRecord('meta-attribute', {
id: Date.now().toString(),
value: 'c',
name: 'middlename',
parent: model,
});
}
}
import Model from 'ember-data/model';
import { belongsTo } from 'ember-data/relationships';
export default class extends Model {
@belongsTo('meta-attribute', { async: false, polymorphic: true, inverse: 'additives' })
parent;
}
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
import { alias } from '@ember/object/computed';
export default class extends Model.extend({
countAdditives: alias('additives.length'),
}) {
@attr('string') value;
@attr('string') name;
@belongsTo('meta-model', { async: false, polymorphic: true, inverse: 'metaAttributes' })
parent;
@hasMany('meta-additive', { async: false, polymorphic: true, inverse: 'parent' })
additives;
}
import Model from 'ember-data/model';
import { belongsTo, hasMany } from 'ember-data/relationships';
import EmberObject, { computed, defineProperty, get } from '@ember/object';
import { alias } from '@ember/object/computed';
export default class extends Model.extend({
allAdditives: computed('metaAttributes.@each.countAdditives', function(){
return this.metaAttributes.toArray().reduce((acc, el)=>{
return acc.concat(el.additives.toArray());
}, []);
})
}) {
@hasMany('meta-attribute', { async: false, inverse: 'parent', polymorphic: true })
metaAttributes;
}
<br>
<button {{on "click" this.pushMetaAdditive}}>Push MetaAdditive</button>
<br>
<br>
<button {{on "click" this.pushMetaAttribute}}><small>Push MetaAttribute</small></button>
<br>
<br><br>
All additives count: {{this.items.firstObject.allAdditives.length}}
<br>
<br>
Issue:
<i>Computed aliases, related to relationships hasMany length is not invalidated if models count changed.</i>
<br>
Steps to reproduce:<br>
<ul>
<li>specify "ember": "3.12.0" (by default for this twiddle)</li>
<li>click on "Push MetaAdditive" button and check "All additives count" updated (for ember-source 3.12)</li>
<li>specify "ember": "3.13.0" </li>
<li>click on "Push MetaAdditive" button and check "All additives count" is NOT updated</li>
<li>click on "Push MetaAttribute" and check, "All additives count" is update</li>
</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.12.0",
"ember-template-compiler": "3.13.0",
"ember-testing": "3.13.0"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-data": "3.12.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment