1 module uim.vue.base.instance; 2 3 import uim.vue; 4 5 @safe: 6 7 @safe: 8 /** A Vue instance is essentially a ViewModel as defined in the MVVM pattern, 9 * hence the variable name vm you will see throughout the docs. 10 * When you instantiate a Vue instance, you need to pass in an options object which can contain options for data, template, 11 * element to mount on, methods, lifecycle callbacks and more. 12 **/ 13 class DVUEInstance : DVUEObj { 14 this() { super(); } 15 this(string aName) { super(aName); } 16 17 mixin(TProperty!("string", "linkTo")); 18 unittest { 19 // 20 } 21 22 /// A hash of components to be made available to the Vue instance. 23 mixin(XStringAA!"components"); 24 unittest { 25 assert(VUEInstance.components("a","b").components == ["a":"b"]); 26 assert(VUEInstance.components(["a":"b"]).components == ["a":"b"]); 27 assert(VUEInstance.components(["a":"b"]).components("x","y").components == ["a":"b", "x":"y"]); 28 /// TODO assert(VUEInstance.components("a","b").components("x","y").removeComponents("a").components == ["x":"y"]); 29 assert(VUEInstance.components(["a":"b"]).components("a","y").components == ["a":"y"]); 30 assert(VUEInstance.components("a","b").clearComponents.components == null); 31 } 32 33 /** 34 * filter - A hash of filters to be made available to the Vue instance. 35 */ 36 mixin(XStringAA!"filters"); 37 unittest { 38 assert(VUEInstance.filters("a","b").filters == ["a":"b"]); 39 assert(VUEInstance.filters(["a":"b"]).filters == ["a":"b"]); 40 assert(VUEInstance.filters(["a":"b"]).filters("x","y").filters == ["a":"b", "x":"y"]); 41 /// TODO: assert(VUEInstance.filters("a","b").filters("x","y").removeFilters("a").filters == ["x":"y"]); 42 assert(VUEInstance.filters(["a":"b"]).filters("a","y").filters == ["a":"y"]); 43 assert(VUEInstance.filters("a","b").clearFilters.filters == null); 44 } 45 46 /** 47 * hooks - All lifecycle hooks: 48 * beforeCreate, created, beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy, destroyed, errorCaptured 49 */ 50 mixin(XStringAA!"hooks"); 51 unittest { 52 assert(VUEInstance.hooks("a","b").hooks == ["a":"b"]); 53 assert(VUEInstance.hooks(["a":"b"]).hooks == ["a":"b"]); 54 assert(VUEInstance.hooks(["a":"b"]).hooks("x","y").hooks == ["a":"b", "x":"y"]); 55 /// TODO assert(VUEInstance.hooks("a","b").hooks("x","y").removeHooks("a").hooks == ["x":"y"]); 56 assert(VUEInstance.hooks(["a":"b"]).hooks("a","y").hooks == ["a":"y"]); 57 assert(VUEInstance.hooks("a","b").clearHooks.hooks == null); 58 } 59 60 /** 61 * plugins - Plugins usually add global-level functionality to Vue. 62 */ 63 mixin(XStringArray!"plugins"); 64 unittest { 65 assert(VUEInstance.plugins("a").plugins == ["a"]); 66 assert(VUEInstance.plugins(["a","b"]).plugins == ["a","b"]); 67 assert(VUEInstance.plugins("a").plugins("x").plugins == ["a", "x"]); 68 //// TODO assert(VUEInstance.plugins("a").plugins("x").removePlugins("a").plugins == ["x"]); 69 assert(VUEInstance.plugins(["a","b"]).clearPlugins.plugins == null); 70 } 71 72 override string[string] settings() { 73 string[string] results = super.settings; 74 75 if (_linkTo) results["el"] = "'#"~_linkTo~"'"; 76 if (_data) results["data"] = _data.toJS(true); 77 if (_components) results["components"] = _components.toJS; 78 if (_filters) results["filters"] = _filters.toJS; 79 if (_hooks) results["hooks"] = _hooks.toJS; 80 if (_plugins) results["plugins"] = _plugins.toJS; 81 82 return results; 83 } 84 85 /// Compare 86 override bool opEquals(string txt) { return toString == txt; } 87 unittest{ 88 assert(VUEInstance == VUEInstance.toString); 89 } 90 91 /// Export to string 92 override string toString() { 93 string result; 94 if (_name) result ~= "var "~_name~"="; 95 result ~= "new Vue("~(settings ? settings.toJS(true) : "")~");"; 96 return result; 97 } 98 } 99 auto VUEInstance() { return new DVUEInstance(); } 100 auto VUEInstance(string aName) { return new DVUEInstance(aName); } 101 102 unittest { 103 /* assert(VUEInstance == "new Vue();"); 104 assert(VUEInstance("app") == "new Vue({el:'#app'});"); 105 assert(VUEInstance.name("app") == "new Vue({el:'#app'});"); 106 assert(VUEInstance.variable("vue").name("app") == "var vue=new Vue({el:'#app'});"); 107 assert(VUEInstance.name("app").data("a", "'b'") == "new Vue({data:{a:'b'},el:'#app'});", 108 "Wrong? -> "~VUEInstance.name("app").data("a", "'b'").toString); 109 assert(VUEInstance.name("app").data("a", "'b'").data(["x": "'y'"]) == "new Vue({data:{a:'b',x:'y'},el:'#app'});", 110 "Wrong? -> "~VUEInstance.name("app").data("a", "'b'").data(["x": "'y'"]).toString); 111 // Test computed 112 assert(VUEInstance.computed("a","return b;") == "new Vue({computed:{a:function(){return b;}}});", 113 "Wrong? -> "~VUEInstance.computed("a","return b;").toString); 114 assert(VUEInstance.computed("a","return b;").computed("x","return z;") == "new Vue({computed:{a:function(){return b;},x:function(){return z;}}});", 115 "Wrong? -> "~VUEInstance.computed("a","return b;").computed("x","return z;").toString); 116 assert(VUEInstance.computed(["a":"return b;", "x":"return z;"]) == "new Vue({computed:{a:function(){return b;},x:function(){return z;}}});", 117 "Wrong? -> "~VUEInstance.computed(["a":"return b;", "x":"return z;"]).toString); 118 // Test methods 119 assert(VUEInstance.methods("a()","return b;") == "new Vue({methods:{a(){return b;}}});", 120 "Wrong? -> "~VUEInstance.methods("a()","return b;").toString); 121 assert(VUEInstance.methods("a()","return b;").methods("x()","return z;") == "new Vue({methods:{a(){return b;},x(){return z;}}});", 122 "Wrong? -> "~VUEInstance.methods("a()","return b;").methods("x()","return z;").toString); 123 assert(VUEInstance.methods(["a()":"return b;", "x()":"return z;"]) == "new Vue({methods:{a(){return b;},x(){return z;}}});", 124 "Wrong? -> "~VUEInstance.methods(["a()":"return b;", "x()":"return z;"]).toString); 125 // Test watch 126 assert(VUEInstance.watch("a","return value;") == "new Vue({watch:{a:function(value){return value;}}});", 127 "Wrong? -> "~VUEInstance.watch("a","return value;").toString); 128 assert(VUEInstance.watch("a","return value;").watch("x","return value;") == "new Vue({watch:{a:function(value){return value;},x:function(value){return value;}}});", 129 "Wrong? -> "~VUEInstance.watch("a","return value;").watch("x","return value;").toString); 130 assert(VUEInstance.watch(["a":"return value;", "x":"return value;"]) == "new Vue({watch:{a:function(value){return value;},x:function(value){return value;}}});", 131 "Wrong? -> "~VUEInstance.watch(["a":"return value;", "x":"return value;"]).toString); 132 */ 133 assert(VUEInstance() == "new Vue();"); 134 assert(VUEInstance("vm") == "var vm=new Vue();"); 135 assert(VUEInstance().linkTo("abc") == "new Vue({el:'#abc'});"); 136 assert(VUEInstance("vm").linkTo("abc") == "var vm=new Vue({el:'#abc'});"); 137 }