var async = require('async'), util = require('util'); var Vue = require('vue'), VueMaterial = require('vue-material'), VueResource = require('vue-resource'); var Light = require('../views/light.vue'); Vue.use(VueMaterial); Vue.use(VueResource); var methods = { toggleSidenav: function () { this.$refs.leftSidenav.toggle(); }, sidenavOpen: function () { console.log('sidenav open'); }, selectLight: function (light) { this.$data.selectedLight = light; this.$refs.leftSidenav.close(); }, // lights refreshLights: function () { var that = this; this.$http.get('lights').then(function (result) { console.log('result', result); async.each(result.body.lights, function (light, callback) { that.$http.get('lights/' + light.id).then(function (result) { var light = result.body; var tmp = that.$data.lights.find(function (l) { return light.id === l.id; }); if (!tmp) { that.$data.lights.push(light); } else { tmp = light; } callback(); }, function (error) { console.log('error', error); callback(); }); }, function () { // init a light selection if (!that.$data.selectedLight.id && that.$data.lights.length > 0) that.$data.selectedLight = that.$data.lights[0]; }); }, function (error) { console.log('error', error); }); }, setLightProperties: function (light) { var that = this; this.$http.put('lights/' + light.id, light.state).then(function (result) { // console.log('result', result); }, function (error) { console.log('error', error); }); } }; var App = new Vue({ el: '#app', http: { root: (process.env.API_ENDPOINT || '') + '/api', }, components: { lightItem: Light }, data: { lights: [], selectedLight: { state: {}} }, methods: methods, mounted: function () { this.refreshLights(); } });