Skip to content

Commit

Permalink
[BUGFIX] Octane: should not need to use get with model.id (#6829)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 authored and igorT committed Dec 18, 2019
1 parent 1d6ce0e commit ae0b964
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 37 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
"test-external:ember-data-relationship-tracker": "./bin/test-external-partner-project.js ember-data-relationship-tracker https://github.com/ef4/ember-data-relationship-tracker.git"
},
"devDependencies": {
"zlib": "1.0.5",
"@babel/plugin-transform-typescript": "^7.7.0",
"@babel/plugin-transform-typescript": "^7.7.4",
"@ember/edition-utils": "^1.1.1",
"@ember/optional-features": "^1.1.0",
"@glimmer/component": "^1.0.0-beta.3",
"@tracerbench/core": "3.0.7",
"@types/ember": "^3.1.1",
"@types/ember-qunit": "^3.4.7",
"@types/ember-test-helpers": "~1.0.6",
Expand Down Expand Up @@ -130,7 +132,9 @@
"rsvp": "^4.8.5",
"semver": "^6.2.0",
"silent-error": "^1.1.1",
"typescript": "~3.6.4"
"typescript": "~3.7.2",
"url": "^0.11.0",
"zlib": "1.0.5"
},
"bin": {
"test-partner-project": "./bin/test-external-partner-project.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
const { setEdition } = require('@ember/edition-utils');

setEdition('octane');

module.exports = {
/**
Ember CLI sends analytics information by default. The data is completely
anonymous, but there are times when you might want to disable this behavior.
Setting `disableAnalytics` to true will prevent any data from being sent.
*/
"disableAnalytics": false
}
disableAnalytics: false,
};
8 changes: 6 additions & 2 deletions packages/-ember-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@ember-data/record-data": "3.15.0-beta.0",
"@ember-data/serializer": "3.15.0-beta.0",
"@ember-data/store": "3.15.0-beta.0",
"@ember/edition-utils": "^1.1.1",
"@ember/ordered-set": "^2.0.3",
"@glimmer/env": "^0.1.7",
"broccoli-merge-trees": "^3.0.2",
Expand All @@ -38,8 +39,10 @@
"ember-inflector": "^3.0.1"
},
"devDependencies": {
"@babel/plugin-transform-typescript": "^7.7.0",
"@babel/plugin-transform-typescript": "^7.7.4",
"@ember-data/unpublished-test-infra": "3.15.0-beta.0",
"@ember/optional-features": "^1.1.0",
"@glimmer/component": "^1.0.0-beta.3",
"@types/ember": "^3.1.1",
"@types/ember-qunit": "^3.4.7",
"@types/ember-test-helpers": "~1.0.6",
Expand Down Expand Up @@ -82,7 +85,8 @@
"json-typescript": "^1.1.0",
"loader.js": "^4.7.0",
"qunit": "^2.9.3",
"typescript": "~3.6.4"
"qunit-dom": "^0.9.2",
"typescript": "~3.7.2"
},
"engines": {
"node": ">= 8.0.0"
Expand Down
99 changes: 99 additions & 0 deletions packages/-ember-data/tests/acceptance/tracking-model-id-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, settled } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Store from '@ember-data/store';
import Model, { attr } from '@ember-data/model';
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Component from '@glimmer/component';
import { gte } from 'ember-compatibility-helpers';
import { has } from 'require';
import { resolve } from 'rsvp';

if (gte('3.13.0') && has('@glimmer/component')) {
class Widget extends Model {
@attr() name;

get numericId() {
return Number(this.id);
}
}

class WidgetList extends Component {
get sortedWidgets() {
let { widgets } = this.args;

return widgets.slice().sort((a, b) => b.numericId - a.numericId);
}
}

let layout = hbs`
<ul>
{{#each this.sortedWidgets as |widget index|}}
<li class="widget{{index}}">
<div class="id">ID: {{widget.id}}</div>
<div class="numeric-id">Numeric ID: {{widget.numericId}}</div>
<div class="name">Name: {{widget.name}}</div>
<br/>
</li>
{{/each}}
</ul>
`;

class TestAdapter extends JSONAPIAdapter {
createRecord() {
return resolve({
data: {
id: '4',
type: 'widget',
attributes: {
name: 'Contraption',
},
},
});
}
}

module('acceptance/tracking-model-id - tracking model id', function(hooks) {
setupRenderingTest(hooks);

hooks.beforeEach(function() {
let { owner } = this;
owner.register('service:store', Store);
owner.register('model:widget', Widget);
owner.register('component:widget-list', WidgetList);
owner.register('template:components/widget-list', layout);
owner.register('adapter:application', TestAdapter);
owner.register('serializer:application', JSONAPISerializer);
});

test("can track model id's without using get", async function(assert) {
let store = this.owner.lookup('service:store');
store.createRecord('widget', { id: '1', name: 'Doodad' });
store.createRecord('widget', { id: '3', name: 'Gizmo' });
store.createRecord('widget', { id: '2', name: 'Gadget' });
this.widgets = store.peekAll('widget');

await render(hbs`
<WidgetList @widgets={{this.widgets}} />
`);
await settled();

assert.dom('ul>li+li+li').exists;
assert.dom('ul>li.widget0>div.name').containsText('Gizmo');
assert.dom('ul>li.widget1>div.name').containsText('Gadget');
assert.dom('ul>li.widget2>div.name').containsText('Doodad');

let contraption = store.createRecord('widget', { name: 'Contraption' });
await contraption.save();
await settled();

assert.dom('ul>li+li+li+li').exists;
assert.dom('ul>li.widget0>div.name').containsText('Contraption');
assert.dom('ul>li.widget1>div.name').containsText('Gizmo');
assert.dom('ul>li.widget2>div.name').containsText('Gadget');
assert.dom('ul>li.widget3>div.name').containsText('Doodad');
});
});
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"template-only-glimmer-components": true,
"application-template-wrapper": false,
"jquery-integration": false
}
8 changes: 7 additions & 1 deletion packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,13 @@ const ID_DESCRIPTOR = {
// the _internalModel guard exists, because some dev-only deprecation code
// (addListener via validatePropertyInjections) invokes toString before the
// object is real.
return this._internalModel && this._internalModel.id;
if (DEBUG) {
if (!this._internalModel) {
return;
}
}
get(this._internalModel, '_tag');
return this._internalModel.id;
},
};

Expand Down
3 changes: 3 additions & 0 deletions packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function extractPivotName(name) {
*/
export default class InternalModel {
_id: string | null;
_tag: number = 0;
modelName: string;
clientId: string;
__recordData: RecordData | null;
Expand Down Expand Up @@ -194,6 +195,7 @@ export default class InternalModel {
if (value !== this._id) {
let newIdentifier = { type: this.identifier.type, lid: this.identifier.lid, id: value };
identifierCacheFor(this.store).updateRecordIdentifier(this.identifier, newIdentifier);
set(this, '_tag', this._tag + 1);
// TODO Show deprecation for private api
}
} else if (!IDENTIFIERS) {
Expand Down Expand Up @@ -1366,6 +1368,7 @@ export default class InternalModel {
let didChange = id !== this._id;

this._id = id;
set(this, '_tag', this._tag + 1);

if (didChange && id !== null) {
this.store.setRecordId(this.modelName, id, this.clientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ember-cli-app-version": "^3.2.0",
"ember-cli-babel": "^7.12.0",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-htmlbars": "^4.0.6",
"ember-cli-htmlbars": "^4.0.8",
"ember-cli-inject-live-reload": "^2.0.2",
"ember-cli-template-lint": "^1.0.0-beta.1",
"ember-export-application-global": "^2.0.0",
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"@ember-data/record-data/*": ["packages/record-data/addon/*"],
"@ember-data/canary-features": ["packages/canary-features/addon"],
"@ember-data/canary-features/*": ["packages/canary-features/addon/*"],
"@ember-data/private-build-infra": ["packages/private-build-infra/addon"],
"@ember-data/private-build-infra/*": ["packages/private-build-infra/addon/*"],
"@ember-data/unpublished-test-infra/test-support": ["packages/unpublished-test-infra/addon-test-support"],
"@ember-data/unpublished-test-infra/test-support/*": ["packages/unpublished-test-infra/addon-test-support/*"],
"fastboot-test-app/tests/*": ["tests/*"],
"fastboot-test-app/*": ["app/*"],
"*": ["packages/store/types/*", "packages/record-data/types/*", "packages/fastboot-test-app/types/*"]
Expand Down
Loading

0 comments on commit ae0b964

Please sign in to comment.