Commits

Tino de Bruijn committed 8d5730e

implemented simple saving

Comments (0)

Files changed (2)

         </header>
         <div data-role="content">
           <h2>Add datapoint</h2>
-          <form data-ajax="false">
+          <form class="save-data" data-ajax="false" feed-id="${feed_id}">
             {{each datastreams}}
             <div data-role="fieldcontain">
-              <label for="datastream-input-${id}-${$value.id}">${$value.unit.label}:</label>
-              <input type="text" name="datastream-input-${id}-${$value.id}" id="datastream-input-${id}-${$value.id}" value="" placeholder="${$value.unit.label}"/>
+              <label for="datastream-input-${feed_id}-${$value.id}">${$value.unit.label}:</label>
+              <input type="text" name="datastream-input-${feed_id}-${$value.id}" id="datastream-input-${feed_id}-${$value.id}" value="" placeholder="${$value.unit.label}"/>
             </div>
             {{/each}}
-            <button type="submit" class="save-data">Save</button>
+            <input type="submit" value="Save">
           </form>
         </div>
         <footer data-role="footer" data-id="feed-${id}-footer" data-position="fixed">
           <h1>${datastream}</h1>
         </header>
         <div data-role="content">
-          <img src="http://api.pachube.com/v2/feeds/${feed_id}/datastreams/${datastream}.png?g=1&w=300&h=380">
+          <img src="http://api.pachube.com/v2/feeds/${feed_id}/datastreams/${datastream}.png?g=1&w=300&h=380&duration=1day">
         </div>
       </div>
     </script>
     /////////////////////
     // Setup of data save button
     
-    $('button.save-data').on('click', function () {
-        elem = $(this);
-        // find the form
-        form = $(elem.parents('form')[0]);
-        alert(form.find('input')[0].value);
+    $('#container').on('submit', 'form.save-data', function (e) {
+        form = $(this);
+        data = {'datastreams': []}
+        form.find('input[type=text]').each(function () {
+            elem = $(this);
+            parts = elem.attr('id').split('-');
+            data['datastreams'].push({'id': parts[3], 'current_value': elem.attr('value')});
+        });
+        data['version'] = '1.0.0'; // pachube api needs this?
+        $.mobile.showPageLoadingMsg();
+        $.ajax({
+            url:  'http://api.pachube.com/v2/feeds/'+form.attr('feed-id'),
+            data: JSON.stringify(data),
+            type: 'PUT',
+            headers: {'X-PachubeApiKey': $.storage.get('api_key')},
+            statusCode: {
+                200: function (data) {
+                        alert('Data saved');
+                        $.mobile.hidePageLoadingMsg();
+                    },
+                401: function() {
+                    alert('unauthorized');
+                    $.mobile.hidePageLoadingMsg();
+                },
+                404: function() {
+                    alert('404: not found');
+                    $.mobile.hidePageLoadingMsg();
+                }
+            },
+        });
+        /*
+            TODO method for 401 (Unauthorized) and 404 ed.
+        */
         e.preventDefault();
+        e.stopPropagation();
         return false;
     })