luci2: implement Class.require() and Class.instantiate()
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 5 Jan 2015 18:16:18 +0000 (19:16 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 5 Jan 2015 18:16:18 +0000 (19:16 +0100)
The two functions allow dynamic loading of additional components.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
luci2/htdocs/luci2/luci2.js

index 284f880b3a34a8eeca0481aa0d290a1cd35a25de..432248c3cc6e768084aa62a0f959a8bc86c6eb90 100644 (file)
@@ -226,6 +226,55 @@ function LuCI2()
                return _class;
        };
 
+       Class.require = function(name)
+       {
+               var path = '/' + name.replace(/\./g, '/') + '.js';
+
+               return $.ajax(path, {
+                       method: 'GET',
+                       async: false,
+                       cache: true,
+                       dataType: 'text'
+               }).then(function(text) {
+                       var code = '%s\n\n//@ sourceURL=%s/%s'.format(text, window.location.origin, path);
+                       var construct = eval(code);
+
+                       var parts = name.split(/\./);
+                       var cparent = L.Class || (L.Class = { });
+
+                       for (var i = 1; i < parts.length - 1; i++)
+                       {
+                               cparent = cparent[parts[i]];
+
+                               if (!cparent)
+                                       throw "Missing parent class";
+                       }
+
+                       cparent[parts[i]] = construct;
+               });
+       };
+
+       Class.instantiate = function(name)
+       {
+               Class.require(name).then(function() {
+                       var parts = name.split(/\./);
+                       var iparent = L;
+                       var construct = L.Class;
+
+                       for (var i = 1; i < parts.length - 1; i++)
+                       {
+                               iparent = iparent[parts[i]];
+                               construct = construct[parts[i]];
+
+                               if (!iparent)
+                                       throw "Missing parent class";
+                       }
+
+                       if (construct[parts[i]])
+                               iparent[parts[i]] = new construct[parts[i]]();
+               });
+       };
+
        this.defaults = function(obj, def)
        {
                for (var key in def)