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

Plugin beforeDefine option #263

Closed
Tape opened this issue Aug 1, 2013 · 3 comments
Closed

Plugin beforeDefine option #263

Tape opened this issue Aug 1, 2013 · 3 comments

Comments

@Tape
Copy link
Contributor

Tape commented Aug 1, 2013

This should be reasonably simple to implement, I'm just pitching the idea.

Let's say I wanted to define a plugin that would serialize an instance to JSON/Javascript with a whitelist or a blacklist. As of right now that is not possible, plus there are currently no model specific settings. Say I defined a plugin with the code below with a beforeDefine hook.

function Plugin (db) {
    return {
        // This uses the properties and opts passed into the Model constructor.
        beforeDefine: function (name, properties, opts) {
            // Now we can intercept and add the methods we want.
            if (!Array.isArray(opts.visible)) {
                return;
            }

            opts.methods = opts.methods || {};
            opts.methods.toJs = function () {
                // Code to filter model instance fields based on the fields provided in the opts.visible Array
            };
            opts.methods.toJson = function () {
                return JSON.serialize(opts.methods.toJs());
            };
        }
    };
}

Now that is all possible. Does this seem like a reasonable idea to you guys?

@dresende
Copy link
Owner

dresende commented Aug 1, 2013

Yes, it seems :) I'll have this available soon

@notheotherben
Copy link
Collaborator

Would definitely come in handy for certain workflows, 👍 from my side

dresende added a commit that referenced this issue Aug 1, 2013
Prototype: beforeDefine(name, properties, options)
@dresende dresende closed this as completed Aug 1, 2013
@Tape
Copy link
Contributor Author

Tape commented Aug 1, 2013

Awesome, thank you!

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

No branches or pull requests

3 participants