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

beforeDestroy & afterDestroy #957

Closed
ifavo opened this issue Oct 7, 2013 · 10 comments
Closed

beforeDestroy & afterDestroy #957

ifavo opened this issue Oct 7, 2013 · 10 comments

Comments

@ifavo
Copy link

ifavo commented Oct 7, 2013

Hi!

I am trying to make a use of before- and afterDestroy on the models but I am struggling to get the object instance that is being destroyed.

How can I get the current dataset that is being deleted? With the criteria on the beforeDestroy function I could do a database query but for this I am missing the classname.

Thanks!
Mario

@sgress454
Copy link
Member

As you said, you should be able to do a query to get the items that would be deleted in "beforeDestroy". Since the callback lives inside the model class file, you should know which model class to use!

// User.js
module.exports = {

  attributes: {     
     name: "string",
     ...etc..
  },

  beforeDestroy: function(criteria, cb) {
     User.find(criteria).exec(function(err, usersToDestroy) {
       // do some stuff, then call cb() to continue or cb(<some error>) to back out
     });
  }
}

afterDestroy doesn't provide information about which items were destroyed.

@ifavo
Copy link
Author

ifavo commented Oct 8, 2013

I was trying to avoid using named models because when renaming or duplicating the model, I will need to modify the contents too.

I was expecting something like:

this.find(criteria).…
this.Model.find(criteria).…

@sgress454
Copy link
Member

I'm not sure I follow. The beforeDestroy method lives in individual model classes, so they will only be called for one type of model. The beforeDestroy inside User.js will only ever be used for User models, and User is a global so you'll always have access to it. To change values of a model before updating it, use beforeUpdate and modify the values hash that is sent as an argument (rather than modifying this).

@ifavo
Copy link
Author

ifavo commented Oct 8, 2013

Yes, that I understood. Thanks for trying to help me understand! :-)

Since I am in the context of modifying something, I would expect to have a simple way to identify it like afterCreate is being providing with the newly created object I was expecting beforeCreate to behave the same way or have a possibility without hard coding names.

I was trying to do the following:

  1. create a Model with some functionality that links to other data, lets say an avatar picture on the filesystem
  2. when beforeDestroy is called I want to delete the file, based on the filename in one of the objects property I am going to destroy
  3. now I want to re-use the Model of User.js for Clients.js, so I duplicate the file and name the new one Clients.js
  4. when beforeDestroy is now being called in one of both Models, it does not know which one it resides in so I need to modify the content of both to hard code the information that this is a different model now (query User or Client to delete the right files)

Also if I ever think about renaming User.js I always need to remember to edit the content to make sure beforeDestroy uses the new model name, I am not free to simple change the model names without checking contents.

I am happy to create workarounds but if there is a solution to identify the models name without hard coding it into the source code, I would love to know it :-)

@Thomasdezeeuw
Copy link

@ifavo if you assign a var to the module.export, like so:

var self = module.exports = {};

you can use self.find(), now you don't have to change it on every rename of the file.

@sgress454
Copy link
Member

Good idea, @Thomasdezeeuw. In fact, you don't even need the var self declaration; you can reference the module directly with module, e.g. module.find.

@gadelkareem
Copy link

afterDestroy on the docs has the destroyed record in the params http://sailsjs.org/#/documentation/concepts/ORM/Lifecyclecallbacks.html?q=callbacks-on-destroy

@Josebaseba
Copy link
Contributor

@gadelkareem that's because now Sails version is the 0.10.x and this issue is from 0.9 or maybe from an earlier version...

@gadelkareem
Copy link

hmm.. I am using sails 0.10.x and afterDestroy does not return record as per docs

@mzalazar
Copy link

mzalazar commented Jan 7, 2015

Strange, this is working fine for me, i'm using 0.10.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants