Commits

Matthew Schinckel committed 5dab846

Moved knockout garmin viewmodel into this project.

Comments (0)

Files changed (1)

contrib/knockout.garmin.js

+var GarminViewModel = function GarminViewModel() {
+  var plugin = new Garmin.Communicator();
+  var devices = ko.observableArray([]);
+  var selectedDevice = ko.observable(null);
+  var busy = ko.observable(false);
+  
+  // Kick off a findDevices, store the found devices in devices.
+  plugin.findDevices(devices);
+  
+  // Observable properties
+  this.busy = ko.computed(function(){
+    return busy();
+  });
+  
+  this.selectedDevice = ko.computed({
+    read: selectedDevice,
+    write: function(data) {
+      var index = devices.indexOf(data);
+      if (index === -1) { index = null; }
+      plugin.selectDevice(index);
+      selectedDevice(data);
+    }
+  });
+  
+  this.deselectDevice = function() {
+    plugin.selectDevice(null);
+    selectedDevice(null);
+  }
+  
+  this.devices = ko.computed(function() {
+    return devices();
+  });
+  
+  this.needSelection = ko.computed(function() {
+    return !selectedDevice() && devices().length > 1;
+  });
+  
+  this.readActivities = plugin.readActivities;
+  
+  // Remove after debugging.
+  this._plugin = plugin;
+};