From bce2de2dd8c7ebf6b06b454c052e68276bd28ab0 Mon Sep 17 00:00:00 2001 From: Mike Larsson Date: Fri, 17 May 2013 16:45:33 -0400 Subject: [PATCH] This change allows the api paginate method to pass through empty data, and introduces a no_data block into the templates to render a message when no data is found on the badges or programs pages, instead of erroring out. --- api.js | 4 +-- test/api-middleware.test.js | 13 ++++++++ views/badges/list.html | 4 +++ views/filter.html | 60 ++++++++++++++++++++----------------- views/programs/list.html | 4 +++ 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/api.js b/api.js index 3f1912e..54e83ef 100644 --- a/api.js +++ b/api.js @@ -141,12 +141,12 @@ function paginate(key, dataFn) { if (err) return callback(err, data); - if (!data[key].length) + if (typeof data[key].length !== 'number') return callback(new errors.BadGateway('Unpageable data returned from upstream'), data); var pages = Math.ceil(data[key].length / pageSize); - if (page > pages) + if (pages > 0 && page > pages) return callback(new errors.NotFound('Page not found'), { page: page, pages: pages diff --git a/test/api-middleware.test.js b/test/api-middleware.test.js index ddc14bb..4e241d6 100644 --- a/test/api-middleware.test.js +++ b/test/api-middleware.test.js @@ -503,6 +503,19 @@ test('paginate', function(t) { t.end(); }); + t.test('don\'t error out on no data', function(t) { + var method = sinon.stub().callsArgWith(1, null, { data: [] });; + var api = new Api(ORIGIN, { + method: { func: method, paginate: true } + }); + var callback = sinon.stub(); + api.method(callback); + var args = callback.getCall(0).args; + t.notOk(args[0], 'no error'); + t.similar(args[1], { data: [], pages: 0 }, 'data'); + t.end(); + }); + t.test('calls paginated method with query and callback', function(t) { var method = sinon.stub(); var api = new Api(ORIGIN, { diff --git a/views/badges/list.html b/views/badges/list.html index f538ad1..2fda877 100644 --- a/views/badges/list.html +++ b/views/badges/list.html @@ -4,4 +4,8 @@ {% block item_actions %} {{ super() }} +{% endblock %} + +{% block no_data %} + No badges found. {% endblock %} \ No newline at end of file diff --git a/views/filter.html b/views/filter.html index 8ad74eb..5291880 100644 --- a/views/filter.html +++ b/views/filter.html @@ -38,33 +38,39 @@ {% endif %} {% endblock %} {% block list_wrapper %} - + {% if items.length %} + + {% else %} + {% block no_data %} + No data found. + {% endblock %} + {% endif %} {% endblock %} {% block pagination %} {% if pages > 1 %} diff --git a/views/programs/list.html b/views/programs/list.html index 4cac05a..a41ce90 100644 --- a/views/programs/list.html +++ b/views/programs/list.html @@ -5,4 +5,8 @@ {% block item_actions %} {{ super() }} +{% endblock %} + +{% block no_data %} + No programs or activities found. {% endblock %} \ No newline at end of file