main.js 1.8 KB

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