From 78a7c389b6c266e18c53a2994403d321019e2f23 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Mon, 9 Jun 2014 00:18:39 +0000 Subject: [PATCH] Handle invalid post ids in editor route No issue -fail fast if an invalid post id is passed into the editor route to prevent an unnecessary network request for all posts -if post id is valid but post does not exist, transition to Content screen instead of returning an invalid model --- core/client/routes/editor.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/core/client/routes/editor.js b/core/client/routes/editor.js index c4cb60b4c4..9fec599d82 100644 --- a/core/client/routes/editor.js +++ b/core/client/routes/editor.js @@ -5,16 +5,32 @@ var EditorRoute = AuthenticatedRoute.extend(styleBody, { classNames: ['editor'], model: function (params) { - var post = this.store.getById('post', params.post_id); + var self = this, + post, + postId; + + postId = Number(params.post_id); + + if (!Number.isInteger(postId) || !Number.isFinite(postId) || postId <= 0) { + this.transitionTo('posts.index'); + } + + post = this.store.getById('post', postId); if (post) { return post; } return this.store.filter('post', { status: 'all', staticPages: 'all' }, function (post) { - return post.get('id') === params.post_id; + return post.get('id') === postId; }).then(function (records) { - return records.get('firstObject'); + var post = records.get('firstObject'); + + if (post) { + return post; + } + + return self.transitionTo('posts.index'); }); } });