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

Allow custom toCSV function for exporting #278

Closed
JedWatson opened this issue Apr 5, 2014 · 2 comments
Closed

Allow custom toCSV function for exporting #278

JedWatson opened this issue Apr 5, 2014 · 2 comments

Comments

@JedWatson
Copy link
Member

We should add support for a custom .toCSV method to be specified on List schemas, and if present, use it to generate the data for export when csvs are downloaded in the Admin UI.

It should use dependency injection to allow (a) the current req or user to be passed in, and (b) a callback to be provided.

If a callback is required, then the data should be built up asynchronously - this would allow other queries to be run when generating the csv data.

Otherwise, the data should be returned.

@JedWatson
Copy link
Member Author

Implemented.

An example of a custom, asynchronous toCSV method on Posts (from the yeoman generator) that populates categories and converts them to keys:

Post.schema.methods.toCSV = function(callback) {

    var post = this,
        rtn = this.toJSON();

    delete rtn.image;

    this.populate('categories', function() {
        rtn.categories = post.categories.map(function(i) { return i.key });
        callback(null, rtn);
    });

};

An example of a simple (non-async) toCSV method whose results are customised for the current user:

Post.schema.methods.toCSV = function(user) {

    return {
        id: post.id,
        name: post.name,
        yours: (user.id == this.author) ? true : false
    };

};

@bladey
Copy link
Contributor

bladey commented Apr 8, 2014

This is a really great feature, thanks Jed.

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