jeanfredrik / meteor-denormalize

Provides simple methods for common denormalization tasks
13 stars 5 forks source link

conflict with my own hooks? ??? #8

Open thearabbit opened 9 years ago

thearabbit commented 9 years ago

For example I have hooks on Comments such as before.insert() and before.update(). When I click on submit to insert, both of my hooks run all at the same time. Please help me.

jeanfredrik commented 9 years ago

I'm not sure what you mean. All "before hooks" run directly after eachother. The "Denormalize hooks" run after any "after hooks" inside a Meteor.defer. Depending on what you do in your "before hooks", you might trigger another "Denormalize hook" run though...

thearabbit commented 9 years ago

I create own hook like this

// Server
Comments.before.insert(function (userId, doc) {
    console.log('before insert');
});
Comments.before.update(function (userId, doc, fieldNames, modifier, options) {
    console.log('before update');
});

When I click insert via autoform, It run on both hook

before insert
before update

And then I try remove .cacheDoc() on Comments, it work fine.

jeanfredrik commented 9 years ago

Ah, that's because .cacheDoc() calls Comments.update(), so before.update is called everytime a cached field updates.

thearabbit commented 9 years ago

I do need this options. You could try direct.update(), I am not sour (I discussed in meteor forums about this).

thearabbit commented 9 years ago

Or you have any solutions, I will waiting for you updated.

jeanfredrik commented 9 years ago

You can use the fieldNames to skip the hook if it's just the cache field that's being updated:

Comments.before.update(function (userId, doc, fieldNames, modifier, options) {
    if(_.without(fieldNames, '_post').length > 0) {
        console.log('before update');
    }
});
thearabbit commented 9 years ago

Could you update your package to support this???

thearabbit commented 9 years ago

I use Collection Hook for all Collections, so I do this (above) for many time.

thearabbit commented 9 years ago

It have problem or not, if I update only reference field (not any fields).

jeanfredrik commented 9 years ago

Sorry for the late reply. Is this still an issue for you or have you been able to resolve it?