question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attributes set at create-time are always changed

See original GitHub issue

Consider the model:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
  country: attr('string'), // e.g. US
  region: attr('string'), // e.g. Illinois
  locality: attr('string'), // e.g. Chicago
  street: attr('string'), // e.g. 1060 W Addison St
  postalCode: attr('string') // e.g. 60613
});

What I’m finding is that any attributes that are set at create-time are always changed, even if auto is set to false, e.g.:

let address = store.createRecord('address', { country: 'AR' });
address.didChange('country'); // => true

I would expect the tracker to start after the record is created when auto is true. Even more frustrating, I can’t set auto to false and call address.startTrack() after createRecord() to do it manually.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
danielspanielcommented, Jul 27, 2017
// utility methods
export function valuesEqual(value1, value2) {
  return (value1 && value1.toString()) === (value2 && value2.toString());
}

export function valuesChanged(value1, value2) {
  let valuesBlank = isEmpty(value1) && isEmpty(value2);
  return !(valuesBlank || valuesEqual(value1, value2));
}


// mixin used in controller
export default Ember.Mixin.create({ 
  initialModelProperties: {},

  setupInitialProperties() {
    let model = this.get('model');
    // using ember-data-change-tracker for changed, but you can use changedAttributes 
    // too if it is only attributes you are checking
    let changedKeys = Object.keys(model.changed());  
    this.set('initialModelProperties', model.getProperties(changedKeys));
  },

  // when you need to see if anything changed
  haveUnsavedChanges() { 
    let props = this.get('initialModelProperties') || {},
        propKeys = Object.keys(props),
        model = this.get('model'),
        changedKeys = Object.keys(model.changed()),
        searchKeys = Ember.merge(propKeys, changedKeys);
    return searchKeys.any((key) => valuesChanged(model.get(key), props[key]));
  }
});

// here is a route that makes a new model as its "model" and passes that to controller
export default Ember.Route.extend({
  model() {
    let code = this.get('session.code');
    let openDate = moment().startOf('day').toDate();
    // i am setting up the model with some initial properties
    return this.store.createRecord('my-model', { openDate, code });
  },

  setupController(controller, model) {
    controller.set('my-model', model);
    controller.setupInitialProperties();  // tell controller to setup initial properties ( from mixin above ) 
  }
});

How’s that?

0reactions
danielspanielcommented, Aug 15, 2017

Your welcome Mike, and I realized the other day that I actually could do the thing where create attributes are set up for new record the way you want them to ( they would be not be considered changed ) etc.

The thing is that this is different than what ember data default does. So it would be a special mode ( I am thinking )

where you would create record and call => model.startTrack() and if the model is new … it would wipe the slate and call current attributes the current state ( and ignore what ember data “changedAttributes” say )

created records can’t be rolled back , so it would not cause an trouble there.

anyway … this would solve a huge headache ( and all the ridiculous hocus pokus from the hacking I showed a few messages above this one )

I don’t really need this but if you need it we can ponder the goodness or badness of such a concept.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting/changing the ctime or "Change time" attribute on a file
1st Method: Changing the kernel.​​ The notify_change function is executed after a file has been changed, where the time metadata needs to be ......
Read more >
How to persist creation and update timestamps with Hibernate
When a new entity gets persisted, Hibernate gets the current timestamp from the VM and sets it as the value of the attribute...
Read more >
11.2.5 Automatic Initialization and Updating for TIMESTAMP ...
TIMESTAMP and DATETIME columns can be automatically initialized and updated to the current date and time (that is, the current timestamp).
Read more >
Can a file appear to be modified before it was created?
The most important thing to note here is that file modification time isn't really special - it's stored just like any other data....
Read more >
How To Change A File/Folder Date Timestamp Using ...
Clicking Change Attributes will open the Attribute Changer in a new window showing the details of the item that you have selected. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found