Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init event fires before leaving empty state #4509

Closed
fivetanley opened this issue Aug 23, 2016 · 2 comments
Closed

init event fires before leaving empty state #4509

fivetanley opened this issue Aug 23, 2016 · 2 comments
Labels
🏷️ bug This PR primarily fixes a reported issue

Comments

@fivetanley
Copy link
Member

Consider the following model definition:

import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import Ember from 'ember';

export default Model.extend({
  title: attr(),
  propSetFromObservingTitle: attr(),

  setPropFromTitle: Ember.observer('title', function() {
      this.set('propSetFromObservingTitle', this.get('title') + 'hi');
  }).on('init')
});

In this example, we have two properties that are DS.attr's. We have propSetFromObservingTitle because we basically want a computed property that also gets serialized to the server.

This code works with normal ember objects, but when we run the following code:

this.store.createRecord('post', {title: 'foo'});

We get the following exception:

VM423 ember.debug.js:31321 Error: Attempted to handle event `didSetProperty` on <post:null> while in state root.empty. Called with {name: propSetFromObservingTitle, oldValue: undefined, originalValue: undefined, value: undefinedhi}.
    at new Error (native)
    at Error.EmberError (https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.6.2/ember.debug.js:15976:21)
    at InternalModel._unhandledEvent (https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.7.0/ember-data.js:2623:13)
    at InternalModel.send (https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.7.0/ember-data.js:2501:14)
    at Class.set (https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.7.0/ember-data.js:13361:31)
    at Descriptor.computedPropertySet [as _set] (https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.6.2/ember.debug.js:14654:28)
    at Descriptor.computedPropertySetWithSuspend [as setWithSuspend] (https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.6.2/ember.debug.js:14634:19)
    at Descriptor.computedPropertySetEntry [as set] (https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.6.2/ember.debug.js:14612:17)
    at Object.set (https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.6.2/ember.debug.js:20348:12)
    at Class.set (https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.6.2/ember.debug.js:34672:38)

Example on Ember Twiddle: https://ember-twiddle.com/415538f55bbaa01ca95067ec0e890d43?openFiles=models.post.js%2C

@runspired
Copy link
Contributor

The issue here is that when we call create we don't do so with the initial or current data, but should. Looking through the code this afternoon I think this is fixable.

@wecc wecc added the Bug label Oct 24, 2016
@stefanpenner
Copy link
Member

https://github.com/emberjs/data/blob/master/addon/-private/system/store.js#L368-L377 should be unified to load + initial the object in one go

stefanpenner added a commit that referenced this issue Apr 18, 2017
[BUGFIX beta] [fixes #4509] createRecord initializes correctly
@runspired runspired added 🏷️ bug This PR primarily fixes a reported issue and removed Bug labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bug This PR primarily fixes a reported issue
Projects
None yet
Development

No branches or pull requests

4 participants