Skip to content

Instantly share code, notes, and snippets.

@IgorKvasn
Last active July 20, 2018 13:05
Show Gist options
  • Save IgorKvasn/a1db4f46721b8fd631eedee6d7a6d705 to your computer and use it in GitHub Desktop.
Save IgorKvasn/a1db4f46721b8fd631eedee6d7a6d705 to your computer and use it in GitHub Desktop.
model bug
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
store: Ember.inject.service(),
init(){
this._super(...arguments);
let entity =
this.store.pushPayload({
data: {
// primary data for single record of type `Person`
id: '1',
type: 'entity',
attributes: {
b: 42
}
}
})
console.log(entity.get('rel.isDirty'));
}
});
import { isPresent } from '@ember/utils';
import { get } from '@ember/object';
import Mixin from '@ember/object/mixin';
export default Mixin.create({
isModelDirty() {
let changed = Object.keys(this.changedAttributes());
let isDirty = false;
get(this.constructor, 'attributes').forEach(attr =>{
if (attr.isAttribute === true && isPresent(attr.options) && attr.options.ignoreDirty !== true){
if (changed.includes(attr.name)){
isDirty = true;
}
}
});
return isDirty;
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
import { getOwner } from '@ember/application';
export default Model.extend({
rel: belongsTo('entity2'),
ready(){
this._super(...arguments);
let store = getOwner(this).lookup('service:store');
this.set('rel', store.createRecord('entity2'));
this.set('rel.isDirty', false);
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
import { getOwner } from '@ember/application';
import ModelDirtyMixin from "../mixins/model-dirty-mixin";
import { computed } from '@ember/object';
export default Model.extend(ModelDirtyMixin,{
hello:attr('string'),
rel2: belongsTo('entity3'),
isDirty: computed(function () {
return this.isModelDirty();
}).volatile(),
ready(){
this._super(...arguments);
let store = getOwner(this).lookup('service:store');
this.set('rel2', store.createRecord('entity3'));
this.set('rel2.isDirty', false);
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
a: attr('string')
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
import { run } from '@ember/runloop';
export default function destroyApp(application) {
run(application, 'destroy');
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
import { start } from 'ember-cli-qunit';
setResolver(resolver);
start();
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "2.18.0",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment