From 1a64cd50712c77d04c47ff55c9093f99e0390587 Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Thu, 16 Feb 2017 20:00:26 +0100 Subject: [PATCH] Fixes problems with owner's id retrieval in the view The retrieval of the owner's id from the URL now works properly. A function has been added to PetView to make it easier to retrieve values from the URL. --- src/main/webapp/js/dao/pet.js | 22 +++------------------- src/main/webapp/js/view/pet.js | 11 ++++++++++- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/main/webapp/js/dao/pet.js b/src/main/webapp/js/dao/pet.js index 5d97ceb..628d28d 100644 --- a/src/main/webapp/js/dao/pet.js +++ b/src/main/webapp/js/dao/pet.js @@ -1,4 +1,4 @@ -var PetDAO = (function(ownerID) { +var PetDAO = (function() { var resourcePath = "rest/pet/"; var requestByAjax = function(data, done, fail, always) { done = typeof done !== 'undefined' ? done : function() {}; @@ -11,18 +11,6 @@ var PetDAO = (function(ownerID) { .always(always); }; - var setOwnerIDPetDAO = function(ownerID, done, fail, always) { - var query = window.location.search.substring(1); - var split = query.split("="); - var getID = decodeURIComponent(split[1]); - //alert(getID); - requestByAjax({ - url: resourcePath + getID, - type: 'POST', - data: getID - }, done, fail, always); - }(); - function PetDAO() { this.listPets = function(done, fail, always) { requestByAjax({ @@ -32,14 +20,10 @@ var PetDAO = (function(ownerID) { }; this.setOwnerIDPetDAO = function(ownerID, done, fail, always) { - var query = window.location.search.substring(1); - var split = query.split("="); - var getID = decodeURIComponent(split[1]); - //alert(getID); requestByAjax({ - url: resourcePath + getID, + url: resourcePath + ownerID, type: 'POST', - data: getID + data: ownerID }, done, fail, always); }; diff --git a/src/main/webapp/js/view/pet.js b/src/main/webapp/js/view/pet.js index 5ce5793..7d2bb50 100644 --- a/src/main/webapp/js/view/pet.js +++ b/src/main/webapp/js/view/pet.js @@ -9,11 +9,20 @@ var PetView = (function() { var listId = 'pet-list'; var formQuery = '#' + formId; var listQuery = '#' + listId; + + var getQueryParameter = function(name) { + var results = new RegExp('[\?&]' + name + '=([^]*)') + .exec(window.location.href); + + return results === null ? null : results[1] || 0; + }; function PetView(petDao, formContainerId, listContainerId) { dao = petDao; self = this; - dao.setOwnerIDPetDAO(); + + dao.setOwnerIDPetDAO(getQueryParameter('id')); + insertPetForm($('#' + formContainerId)); insertPetList($('#' + listContainerId)); -- 2.18.1