main.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var async = require('async'),
  2. util = require('util');
  3. var Vue = require('vue'),
  4. VueMaterial = require('vue-material'),
  5. VueResource = require('vue-resource');
  6. var Light = require('../views/light.vue');
  7. Vue.use(VueMaterial);
  8. Vue.use(VueResource);
  9. var methods = {
  10. toggleSidenav: function () {
  11. this.$refs.leftSidenav.toggle();
  12. },
  13. sidenavOpen: function () {
  14. console.log('sidenav open');
  15. },
  16. selectLight: function (light) {
  17. this.$data.selectedLight = light;
  18. this.$refs.leftSidenav.close();
  19. },
  20. // lights
  21. refreshLights: function () {
  22. var that = this;
  23. this.$http.get('lights').then(function (result) {
  24. console.log('result', result);
  25. async.each(result.body.lights, function (light, callback) {
  26. that.$http.get('lights/' + light.id).then(function (result) {
  27. var light = result.body;
  28. var tmp = that.$data.lights.find(function (l) { return light.id === l.id; });
  29. if (!tmp) {
  30. that.$data.lights.push(light);
  31. } else {
  32. tmp = light;
  33. }
  34. callback();
  35. }, function (error) {
  36. console.log('error', error);
  37. callback();
  38. });
  39. }, function () {
  40. // init a light selection
  41. if (!that.$data.selectedLight.id && that.$data.lights.length > 0) that.$data.selectedLight = that.$data.lights[0];
  42. });
  43. }, function (error) {
  44. console.log('error', error);
  45. });
  46. },
  47. setLightProperties: function (light) {
  48. var that = this;
  49. this.$http.put('lights/' + light.id, light.state).then(function (result) {
  50. // console.log('result', result);
  51. }, function (error) {
  52. console.log('error', error);
  53. });
  54. }
  55. };
  56. var App = new Vue({
  57. el: '#app',
  58. http: {
  59. root: (process.env.API_ENDPOINT || '') + '/api',
  60. },
  61. components: {
  62. lightItem: Light
  63. },
  64. data: {
  65. lights: [],
  66. selectedLight: { state: {}}
  67. },
  68. methods: methods,
  69. mounted: function () {
  70. this.refreshLights();
  71. }
  72. });