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

Default value for empty array #2

Open
Prinzhorn opened this issue Sep 8, 2013 · 1 comment
Open

Default value for empty array #2

Prinzhorn opened this issue Sep 8, 2013 · 1 comment

Comments

@Prinzhorn
Copy link

Thanks for creating screener, I'm using it for our mobile API.

I have an offer object and my filter looks like this

module.exports = {
    _id: true,
    title: true,
    body: true,
    location: true,
    images: [require('../image')]
};

and image looks like

var models = require('models');

module.exports = function(object) {
    return models.Image.getUrl(object);
};

It's working perfectly and transforms the array of image _ids into an array of Strings (the URLs of the images).

Now the thing is I'd like to have at least one image inside the array. That means if the offer object does not have any images (empty array), then I want to react and fill it with the URL of the default image.

It would be great if there's a way to do this with screener as I'd like to keep the logic away from the controller.

@Prinzhorn
Copy link
Author

I've now solved it like this

module.exports = {
    _id: true,
    title: true,
    body: true,
    location: true,
    images: require('../imageList')
};

imageList.js

var imageFilter = require('./image');

module.exports = function(images) {
    if(!images.length) {
        return [imageFilter('default')];
    }

    return images.map(function(image) {
        return imageFilter(image);
    });
};

image.js

var models = require('models');

module.exports = function(_id) {
    return models.Image.getUrl(_id);
};

Tell me if there's a nicer solution. If not I can live with what I have now.

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

1 participant