Browse files

final changes to refactored version of elance sample store for elance…

… test
  • Loading branch information...
1 parent a71e09e commit 8cf1263138ca8ea03ac70d70026f90ea1a80d611 lcaraccioli committed Apr 29, 2012
Showing with 110 additions and 64 deletions.
  1. +10 −1 test_code_problems/task_5_feature_test/processAHR.php
  2. +100 −63 test_code_problems/task_5_feature_test/store.js
View
11 test_code_problems/task_5_feature_test/processAHR.php
@@ -18,9 +18,18 @@
case 'load':
$jsonData = array(
'categories'=>$categories,
- 'storeData'=>$storeData,
);
break;
+ case 'loadProducts':
+ $categoryId = $_REQUEST['catid'];
+ $jsonData['storeData']=array();
+ foreach ($storeData as $product){
+ if ($product['catid'] == $categoryId){
+ $jsonData['storeData'][] = $product;
+ }
+ }
+
+ break;
case 'checkout':
break;
default:
View
163 test_code_problems/task_5_feature_test/store.js
@@ -1,75 +1,112 @@
//Javascript file for question #5
//MooTools documentation can be found at www.mootools.net
-var productData = {};
-function init(){
- var myRequest = new Request({
+var products = [];
+function getCategories(callback){
+ var categoryRequest = new Request({
url:'processAHR.php?action=load',
format:'json',
onSuccess:function(responseText){
- var jsonResponse = JSON.decode(responseText);
- Array.each(jsonResponse.categories, function(item,index,object){
- var option = new Element('option', {value:index});
- option.appendText(item);
- $('category').appendChild(option);
- $('category').addEvent('change', function(){
- $('item').getElements('option').destroy();
- var catid = $('category').get('value');
- productData = jsonResponse.storeData;
- Array.each(jsonResponse.storeData, function(item,index,object){
- if (catid == item.catid){
- var option = new Element('option', {value:item.item_id});
- option.appendText(item.name + ' - $' + item.price);
- $('item').appendChild(option);
- }
- });
- $('item').fireEvent('change');
- });
- $('item').addEvent('change', function(){
- var itemId = $('item').get('value');
- var product = jsonResponse.storeData[itemId-1];
- $('description').set('html',product.description);
- $('product-image').getElement('img').set('src', product.image);
- $('product-name').set('html', product.name+": $"+product.price);
- });
- });
- $('add-to-cart').addEvent('click', function(){
- var itemId = $('item').get('value');
- var cartItemEntry = new Element('tr');
- var itemNameData = new Element('td');
- itemNameData.appendText(jsonResponse.storeData[itemId -1].name);
- var itemPriceData = new Element('td');
- itemPriceData.appendText('$'+jsonResponse.storeData[itemId -1].price);
- var removeLinkColumn = new Element('td');
- var removeButton = new Element('a');
- removeButton.appendText('Remove');
- removeButton.set('href','#');
- removeButton.addEvent('click', function(){
- //remove the parent tr
- removeButton.getParent().getParent().destroy();
- redrawCart();
- });
- var hiddenProductInput = new Element('input');
- hiddenProductInput.set('type', 'hidden');
- hiddenProductInput.set('name', 'products[]');
- hiddenProductInput.set('value', itemId);
- removeLinkColumn.appendChild(removeButton);
- removeLinkColumn.appendChild(hiddenProductInput);
- cartItemEntry.appendChild(itemNameData);
- cartItemEntry.appendChild(itemPriceData);
- cartItemEntry.appendChild(removeLinkColumn);
- $('shopping-cart').getElement('tbody').appendChild(cartItemEntry);
- redrawCart();
- });
- $('checkout').addEvent('click',function(){
- alert('TODO: checkout');
- return false;
+ var categoryJson = JSON.decode(responseText);
+ setupCategories(categoryJson.categories);
+ }
+ });
+ categoryRequest.send();
+}
+
+function getProducts(catid, callback){
+ var productRequest = new Request({
+ url:'processAHR.php?action=loadProducts&catid='+catid,
+ format:'json',
+ onSuccess:function(responseText){
+ productData = JSON.decode(responseText);
+ Array.each(productData.storeData, function (item,index,object){
+ setProduct(item.item_id,item);
});
- $('category').fireEvent('change');
-
+ setupProducts(productData.storeData);
}
});
- myRequest.send();
+ productRequest.send();
+}
+
+function setupProducts(products){
+ $('item').getElements('option').destroy();
+ Array.each(products, function(item,index,object){
+ var option = new Element('option', {value:item.item_id});
+ option.appendText(item.name + ' - $' + item.price);
+ $('item').appendChild(option);
+ });
+ $('item').fireEvent('change');
+}
+
+function getProduct(productId){
+ return products[productId];
+}
+
+function setProduct(productId, product){
+ products[productId] = product;
+}
+
+function setupProduct(product){
+ $('description').set('html',product.description);
+ $('product-image').getElement('img').set('src', product.image);
+ $('product-name').set('html', product.name+": $"+product.price);
+}
+
+function setupCategories(categories){
+ $('category').getElements('option').destroy();
+ Array.each(categories, function(item,index,object){
+ var option = new Element('option', {value:index});
+ option.appendText(item);
+ $('category').appendChild(option);
+ });
+ $('category').fireEvent('change');
+}
+
+function init(){
+ $('category').addEvent('change', function(){
+ var catid = $('category').get('value');
+ getProducts(catid);
+ });
+ $('item').addEvent('change', function(){
+ var itemId = $('item').get('value');
+ setupProduct(getProduct(itemId));
+ });
+ $('add-to-cart').addEvent('click', function(){
+ var itemId = $('item').get('value');
+ var cartItemEntry = new Element('tr');
+ var itemNameData = new Element('td');
+ itemNameData.appendText(products[itemId].name);
+ var itemPriceData = new Element('td');
+ itemPriceData.appendText('$'+products[itemId].price);
+ var removeLinkColumn = new Element('td');
+ var removeButton = new Element('a');
+ removeButton.appendText('Remove');
+ removeButton.set('href','#');
+ removeButton.addEvent('click', function(){
+ //remove the parent tr
+ removeButton.getParent().getParent().destroy();
+ redrawCart();
+ });
+ var hiddenProductInput = new Element('input');
+ hiddenProductInput.set('type', 'hidden');
+ hiddenProductInput.set('name', 'products[]');
+ hiddenProductInput.set('value', itemId);
+ removeLinkColumn.appendChild(removeButton);
+ removeLinkColumn.appendChild(hiddenProductInput);
+ cartItemEntry.appendChild(itemNameData);
+ cartItemEntry.appendChild(itemPriceData);
+ cartItemEntry.appendChild(removeLinkColumn);
+ $('shopping-cart').getElement('tbody').appendChild(cartItemEntry);
+ redrawCart();
+ });
+ $('checkout').addEvent('click',function(){
+ alert('TODO: checkout');
+ return false;
+ });
+ getCategories();
+
};
+
function redrawCart(){
if ($('shopping-cart').getElement('tbody').getElements('tr').length <= 0){
$('shopping-cart').setStyle('display','none');

0 comments on commit 8cf1263

Please sign in to comment.