rendrjs / rendr

Render your Backbone.js apps on the client and the server, using Node.js.
MIT License
4.09k stars 312 forks source link

Added ability for each spec to set readFromCache to true/false #469

Closed crwang closed 9 years ago

crwang commented 9 years ago

What/Why

With this PR:

This would be possible

module.exports = {
    index: function(params, callback) {
        var spec = {
            collection: {
                collection: 'Users',
                params: params
            }
        };
        this.app.fetch(spec, function(err, result) {
            callback(err, result);
        });
    },

    show: function(params, callback) {
        var spec = {
            model: {
                model: 'User',
                params: params,
                readFromCache: false
            },
            repos: {
                collection: 'Repos',
                params: {
                    user: params.login
                },
                readFromCache: true // or omit for default
            }
        };
        this.app.fetch(spec, function(err, result) {
            callback(err, result);
        });
    }
};

As before, always reading from cache is still possible

module.exports = {
    index: function(params, callback) {
        var spec = {
            collection: {
                collection: 'Users',
                params: params
            }
        };
        this.app.fetch(spec, function(err, result) {
            callback(err, result);
        });
    },

    show: function(params, callback) {
        var spec = {
            model: {
                model: 'User',
                params: params               
            },
            repos: {
                collection: 'Repos',
                params: {
                    user: params.login
                }
            }
        };
        this.app.fetch(spec, function(err, result) {
            callback(err, result);
        });
    }
};

As before, never reading from cache is still possible

module.exports = {
    index: function(params, callback) {
        var spec = {
            collection: {
                collection: 'Users',
                params: params
            }
        };
        this.app.fetch(spec, function(err, result) {
            callback(err, result);
        });
    },

    show: function(params, callback) {
        var spec = {
            model: {
                model: 'User',
                params: params
            },
            repos: {
                collection: 'Repos',
                params: {
                    user: params.login
                }
            }
        };
        this.app.fetch(spec, { readFromCache: false }, function(err, result) {
            callback(err, result);
        });
    }
};
coveralls commented 9 years ago

Coverage Status

Coverage increased (+0.28%) to 84.5% when pulling 435c7c79994ff21afd7b9f861d357f6cde83f1e7 on crwang:selective-read-from-cache into 5ada76ac4d284008e966037c7e0d86b83190f326 on rendrjs:master.

pjanuario commented 9 years ago

:+1:

saponifi3d commented 9 years ago

Nice! :+1: looks awesome!

I'm probably going to open up a companion PR to this shortly that it'd be awesome if you could take a look at. I'm planning on making it allowing an option to be be passed to constructors as well to save to the cache or not.