Skip to content

Plugin beforeDefine option #263

@Tape

Description

@Tape

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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions