diff --git a/extensions/web-scripts/view-init.js b/extensions/web-scripts/view-init.js index 392eb0c..4c779bb 100644 --- a/extensions/web-scripts/view-init.js +++ b/extensions/web-scripts/view-init.js @@ -1,31 +1,5 @@ define(['./config.json', './view.xml'], function(config, viewXml) { - function chainInitFn(f1, f2) { - return function(a) { f1(a); f2(a); }; - } - - var initFn = function() {}; - for(;;) { - var i = viewXml.indexOf('', i); - if (j < 0) { - break; - } - var src = viewXml.substring(i + 8, j); - viewXml = viewXml.substring(0, i) + viewXml.substring(j + 9); - var fn = Function('view', '"use strict"; ' + src); - initFn = chainInitFn(initFn, fn); - } - - var viewTemplate = [ - '', - viewXml, - '' - ].join('\n'); - function getPropertyValue(things, properties, path) { var parts = path.split('/', 2); var thingId = parts[0]; @@ -56,61 +30,88 @@ define(['./config.json', './view.xml'], function(config, viewXml) { return undefined; } - var viewVue = new Vue({ - template: viewTemplate, - data: { - property: {} - }, - methods: { - onShow: function() { - this.onDataChange(); + var script = ''; + for(;;) { + // type="text/javascript" + var i = viewXml.indexOf('', i); + if (j < 0) { + break; + } + script += '\n' + viewXml.substring(i + 8, j); + viewXml = viewXml.substring(0, i) + viewXml.substring(j + 9); + } + + var options = { + template: [ + '', + viewXml, + '' + ].join('\n') + }; + + if (config.properties && config.properties.length > 0) { + assignMap(options, { + data: { + property: {} }, - onDataChange: function() { - Promise.all([ - app.getThings(), - app.getPropertiesByThingId() - ]).then(apply(this, function(things, properties) { - var propertyMap = {}; + methods: { + onShow: function() { + this.onDataChange(); + }, + onDataChange: function() { + Promise.all([ + app.getThings(), + app.getPropertiesByThingId() + ]).then(apply(this, function(things, properties) { + var propertyMap = {}; + for (var i = 0; i < config.properties.length; i++) { + var cfgProp = config.properties[i]; + propertyMap[cfgProp.name] = getPropertyValue(things, properties, cfgProp.path); + } + this.property = propertyMap; + })); + }, + getPropertyByPath: function(path) { + return Promise.all([ + app.getThings(), + app.getPropertiesByThingId() + ]).then(apply(this, function(things, properties) { + return getPropertyValue(things, properties, path); + })); + }, + setPropertyByPath: function(path, value) { + var parts = path.split('/', 2); + var thingId = parts[0]; + var propName = parts[1]; + var valueByName = {}; + valueByName[propName] = value; + return fetch('/things/' + thingId + '/properties', { + method: 'PUT', + body: JSON.stringify(valueByName) + }).then(rejectIfNotOk); + }, + setProperty: function(name, value) { for (var i = 0; i < config.properties.length; i++) { var cfgProp = config.properties[i]; - propertyMap[cfgProp.name] = getPropertyValue(things, properties, cfgProp.path); - } - this.property = propertyMap; - })); - }, - getPropertyByPath: function(path) { - return Promise.all([ - app.getThings(), - app.getPropertiesByThingId() - ]).then(apply(this, function(things, properties) { - return getPropertyValue(things, properties, path); - })); - }, - setPropertyByPath: function(path, value) { - var parts = path.split('/', 2); - var thingId = parts[0]; - var propName = parts[1]; - var valueByName = {}; - valueByName[propName] = value; - return fetch('/things/' + thingId + '/properties', { - method: 'PUT', - body: JSON.stringify(valueByName) - }).then(rejectIfNotOk); - }, - setProperty: function(name, value) { - for (var i = 0; i < config.properties.length; i++) { - var cfgProp = config.properties[i]; - if (cfgProp.name === name) { - return this.setPropertyByPath(cfgProp.path, value); + if (cfgProp.name === name) { + return this.setPropertyByPath(cfgProp.path, value); + } } + return Promise.reject('not found'); } - return Promise.reject('not found'); } - } - }); - - addPageComponent(viewVue, config.icon); + }); + } - initFn(viewVue); + if (script.length > 0) { + var fn = Function('options', 'config', '"use strict";' + script); + fn.call(this, options, config); + } + var vue = new Vue(options); + addPageComponent(vue, config.icon); }); \ No newline at end of file