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

Intercepting beforeRemove or remove events #227

Open
ivanseidel opened this issue Sep 4, 2014 · 6 comments
Open

Intercepting beforeRemove or remove events #227

ivanseidel opened this issue Sep 4, 2014 · 6 comments

Comments

@ivanseidel
Copy link

My model looks like this:

var Photo = function () {

    // Url to photo (Hosted in Cloudinary)
    this.property('publicId', 'string');

    this.afterCreate = function (){
        console.log('AFTER CREATE', this);
        this.on('beforeRemove', function (model, values) {

            console.log('DELETING', values, this);
            // Check if image is uploaded. If so, remove from cloudinary
            if(values.publicId){
            console.log('DELETING', values);
                geddy.cloudinary.api.delete_resources([values.publicId]);
            }
        });
    }
};

It should be listening for beforeRemove events, but it isn't... The max I get, is the afterCreate callback.

Have tried this before, but without succeeding (inside the model):

this.beforeRemove = function (){
   ...
};

Is there anything in the docs explaining this?

Thanks

@mde
Copy link
Contributor

mde commented Sep 7, 2014

This might be counter-intuitive but "beforeRemove" is a static event emitted by the constructor (the model-definition), and the afterCreate method is on the instances themselves. So inside your afterCreate method, "this" points to the instances.

You can try creating a listener on the constructor itself like this:

geddy.model.Photo.on('beforeRemove', function () { // Do stuff });

@ivanseidel
Copy link
Author

I found out the problem...

The EventEmitter is only injected to the model after the geddy.model.register('Photo', Photo);

Placing the Photo.on('beforeRemove', cb) inside the photo.js Model file, must be done AFTER registering it...

(I wanted the callbacks inside the model file)

Thanks!

@mde
Copy link
Contributor

mde commented Sep 7, 2014

Glad you were able to figure it out!

@ivanseidel
Copy link
Author

@mde , there is a problem that I'm facing right now...
the Event is emited with just the ID, not the object itself, and the 'this' context is binded to the model constructor, not the item itself.

Because of that, I'm not able to do async stuff, because i don't have the model properties...

Just found a bug?

@mde
Copy link
Contributor

mde commented Sep 27, 2014

The 'beforeRemove' might be better implemented as an instance method, yes. Let's reopen and reexamine.

@mde mde reopened this Sep 27, 2014
@ivanseidel
Copy link
Author

In my mind, it should follow the same patter as before[Something], witch is: calling the event handler with a parameter that is the model itself.

The problem with that, is that it won't be compatible with older versions (since the current one callsback with the Id only).

So, we have two options that I can think about:

  • beforeRemove(id, model) (won't break any code)
  • beforeRemove(model) (breaks who uses the id, but it's fixed simply with with model.id)

What do you think?

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

No branches or pull requests

2 participants