JackAdams / meteor-undo-redo

Undo/Redo for Meteor
http://transactions.meteor.com/
MIT License
4 stars 0 forks source link

autoValue #1

Open lynchem opened 8 years ago

lynchem commented 8 years ago

Guys, great package. I'm really impressed. This is a great use of the transactions. The only issue I'm having is with autoValue. I use this for updating an updatedAt and createdAt amongst other things.

This works

This doesn't

Here are my autovalues

    createdAt: {
        type: Date,
        optional: true,
        autoValue: function() {
            if (Meteor.isServer) {
                if (this.isInsert) {
                    return new Date;
                } else if (this.isUpsert) {
                    return {$setOnInsert: new Date};
                } else {
                    this.unset();
                }
            }
        }
    },
    // Force value to be current date (on server) upon update
    // and don't allow it to be set upon insert.
    updatedAt: {
        type: Date,
        optional: true,
        autoValue: function() {
            if (Meteor.isServer) {
                if (this.isUpdate) {
                    return new Date();
                }
            }
        }
        //NOTE: We need to disable this to allow the transactions to restore an element.
        //denyInsert: true
    }

It would great if we could whitelist certain properties so they don't interfere with the checks to see if edits have occurred.

Or maybe you have better ideas how to fix this ? The createdAt functionality will also be broken at the moment as restoring an element will give a new createdAt date as opposed to the original.... so maybe it's worth re-thinking this entirely. Any suggestions most welcome as I really think this package has a lot of potential.

lynchem commented 8 years ago

Actually, I tried this on the demo app you have and it doesn't seem to work either so maybe autoValue is a red herring ? I've left the original one as it is as it would be really useful to have a strategy for making autoValues and undo work together.

JackAdams commented 8 years ago

Which package uses the autoValue param? Is it simple-schema or autoform or something like that?

lynchem commented 8 years ago

Sorry, it's SimpleSchema that has it. Basically hooks for actions on insert/update. But like I said, it seems like a red herring as the demo app exhibits the same behaviour

JackAdams commented 8 years ago

There was a bug fix release last night (0.7.8) that dealt with the issue of callbacks not working on inserts with simple schema. I haven't looked at the update callbacks yet, but suspect they might need fixing too.

I'm pretty sure that if you need writes to be initiated directly on the client, rather than through a method call, you'll need to set {tx: true, instant: true}. The instant field makes the write take effect on the client in minimongo immediately and the callback is fired (while the native rpc updates the mongo document on the server). If instant: true isn't set, the update will be bundled up and sent off all other updates to the server for processing, where client-side callback functions can't follow (down the wire).

I hope that makes sense. If the issue persists with inserts let me know -- I don't imagine updates will be working in 0.7.8.

The demo app uses an old version of the package (I haven't had time to update it yet).