|
| 1 | +// Wait until the everythings loaded before setting up the app |
| 2 | +$fh.ready(function () { |
| 3 | + var mashup_button = $('#mashup_button'); |
| 4 | + |
| 5 | + mashup_button.bind('click', function (e) { |
| 6 | + mashup_button.attr('disabled', true).val('loading...'); |
| 7 | + // Get our locally stored hash of the data if we have one |
| 8 | + $fh.data({ |
| 9 | + act: 'load', |
| 10 | + key: 'mashup_hash' |
| 11 | + }, function (res) { |
| 12 | + // We have a hash, lets send this along to the server |
| 13 | + if (null !== res.val) { |
| 14 | + var hash = res.val; |
| 15 | + doMashup(hash); |
| 16 | + } |
| 17 | + else { |
| 18 | + // No hash, so we haven't gotten any data from server yet |
| 19 | + // Lets just call the server and get whatever data it has |
| 20 | + doMashup(); |
| 21 | + } |
| 22 | + }, function (error) { |
| 23 | + alert(error); |
| 24 | + enableMashupButton(); |
| 25 | + }); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | + |
| 30 | +function doMashup(hash) { |
| 31 | + hash = 'undefined' === typeof hash ? '' : hash; |
| 32 | + |
| 33 | + // Call the server side function to get the mashup data |
| 34 | + $fh.act({ |
| 35 | + act: 'getMashup', |
| 36 | + req: { |
| 37 | + hash: hash, |
| 38 | + timestamp: new Date().getTime() |
| 39 | + } |
| 40 | + }, function (res) { |
| 41 | + var mashup = res; |
| 42 | + |
| 43 | + // Check if our hash's match, so we can use the locally cached data |
| 44 | + if (hash === mashup.hash) { |
| 45 | + // we already have the latest data, lets pull it from local storage |
| 46 | + $fh.data({ |
| 47 | + act: 'load', |
| 48 | + key: hash |
| 49 | + }, function (res) { |
| 50 | + if (null !== res.val) { |
| 51 | + // Parse out the data and show it |
| 52 | + var data = JSON.parse(res.val); |
| 53 | + updateMashup(hash, data); |
| 54 | + } |
| 55 | + else { |
| 56 | + // Local data doesn't exist. Notify user and get fresh data instead |
| 57 | + alert('Server said hashes match, but no local data found for ' + hash |
| 58 | + + '. Doing a fresh mashup call to repopulate our local data. (no hash parm passed to ensure server call)'); |
| 59 | + doMashup(); |
| 60 | + } |
| 61 | + }, function (error) { |
| 62 | + alert('Error retrieving mashup data from local storage:' + error); |
| 63 | + enableMashupButton(); |
| 64 | + }); |
| 65 | + } |
| 66 | + else { |
| 67 | + // Hash's don't match, so server has included the newest data to use |
| 68 | + // Lets save it and update our hash value |
| 69 | + $fh.data({ |
| 70 | + act: 'save', |
| 71 | + key: 'mashup_hash', |
| 72 | + val: mashup.hash |
| 73 | + }, function (res) { |
| 74 | + // hash saved - so now save the new mashup data returned |
| 75 | + $fh.data({ |
| 76 | + act: 'save', |
| 77 | + key: mashup.hash, |
| 78 | + val: JSON.stringify(mashup) |
| 79 | + }, function (res) { |
| 80 | + // mashup data saved |
| 81 | + }, function (error) { |
| 82 | + alert(error); |
| 83 | + enableMashupButton(); |
| 84 | + }); |
| 85 | + }, function (error) { |
| 86 | + alert(error); |
| 87 | + enableMashupButton(); |
| 88 | + }); |
| 89 | + // No need to wait for asynchronous saving above to finish. Lets go ahead |
| 90 | + // and show the latest mashup data |
| 91 | + updateMashup(hash, mashup); |
| 92 | + } |
| 93 | + }, function (code,errorprops,params) { |
| 94 | + // Something went wrong with server side function call, let's alert the user |
| 95 | + alert('Failed to get mashup from server. Error:' + code); |
| 96 | + enableMashupButton(); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +function updateMashup(hash, mashup) { |
| 102 | + $('#mashup_myhash').text(hash); |
| 103 | + $('#mashup_serverhash').text(mashup.hash); |
| 104 | + $('#mashup_cached').text(hash === mashup.hash); |
| 105 | + $('#mashup_entries').text(mashup.data.length); |
| 106 | + $('#mashup_data').text(JSON.stringify(mashup.data)); |
| 107 | + |
| 108 | + enableMashupButton(); |
| 109 | +} |
| 110 | + |
| 111 | +function enableMashupButton() { |
| 112 | + $('#mashup_button').removeAttr('disabled').val('Get Mashup from Server'); |
| 113 | +} |
0 commit comments