Giter Site home page Giter Site logo

Comments (6)

OscarGodson avatar OscarGodson commented on August 30, 2024

It does seem tho that looking up this family like this:

    FamilyModel.first({id: query, disabled: false}, {includes: ['users', 'familyMemberships']}, function (err, familyModel) {
      if (err) return callback.call(this, createResponse.error());
      if (!familyModel) return callback.call(this, createResponse.error(404, 'No family found'));
      console.log(familyModel)
    });

returns my expected

{
"message": "No family found"
}

from model.

OscarGodson avatar OscarGodson commented on August 30, 2024

oh...I think I may know what it is... is afterCreate supposed to fire even on reads? It appears as tho it's firing "create" on associated models. This is why it works if you hit it directly, but if you ask for the association its like its creating it new. This also means all my default props im setting in afterCreate are overriding whats actually in the DB

from model.

ben-ng avatar ben-ng commented on August 30, 2024

Yes, create fires even on reads. I'm not sure if this is correct behavior anymore, since we now have the reify scenario.

Since the create method doesn't actually save models to the database (you still have to call save), create is really more of an initialize method, so the behavior is correct if you look at it that way.

See here, where models are initialized during a query using the create method.

I agree that this behavior is confusing though. What do you think @mde?

from model.

OscarGodson avatar OscarGodson commented on August 30, 2024

@ben-ng afterCreate tho is the suggested way of setting defaults as far as i know. Is there some other method or way in the model to set defaults on create?

The hardest part is that this fires only on associated models. If I read Foo model it doesn't fire. If I read Foo model with Bar and Baz associations then Bar and Baz get the create but Foo doesn't. At least if Foo did it then I could account for that a little easier, but currently I have to come up with a way to know if its being called as an association or as a "top level" model.

from model.

OscarGodson avatar OscarGodson commented on August 30, 2024

So, at least for now and in my case, I'm getting around it with a tiny module:

module.exports = function (prop, def) {
  return prop === undefined ? def : prop;
};

I use it like

var setAsModelDefault = require('../utils/set-as-model-default');
this.afterCreate = function () {
  this.disabled = setAsModelDefault(this.disabled, false);
};

Not ideal, but it works for me

from model.

ben-ng avatar ben-ng commented on August 30, 2024

Ahh i see. I'll take a look at making the behavior consistent then. I think the right thing to do is probably to not fire create on reads anymore. Its going to be a breaking change, but its the most logical to me. We should have an init event take its place. Then, init will happen on both queries and user-performed initializations, create only on user-performed initializations, and reify on queries.

from model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.