From 1b916ea9e1f6694009c92a93857d87c371ff0f6c Mon Sep 17 00:00:00 2001 From: Ralph Iden Date: Tue, 26 Jan 2016 21:15:05 -0600 Subject: [PATCH] Add insertPrecheck function and unit tests. --- lib/weld.js | 7 ++++++- lib/weld.min.js | 20 ++++++++++---------- test/test.html | 9 +++++++-- test/test.js | 25 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lib/weld.js b/lib/weld.js index fd35313..1f71516 100644 --- a/lib/weld.js +++ b/lib/weld.js @@ -329,7 +329,9 @@ } } ops.traverse(templateParent, target, i, value[i], value[i]); - ops.insert(templateParent, target, i, value[i]); + if (ops.insertPrecheck(templateParent, target, i, value[i])) { + ops.insert(templateParent, target, i, value[i]); + } } // OBJECT @@ -397,6 +399,9 @@ } }, map : false, // this is a user-defined operation + insertPrecheck : function(parent, element, key, value) { + return true; + }, insert : function(parent, element) { // Insert the template back into document if (element.weld && element.weld.insertBefore) { diff --git a/lib/weld.min.js b/lib/weld.min.js index 0e84223..085be62 100644 --- a/lib/weld.min.js +++ b/lib/weld.min.js @@ -1,10 +1,10 @@ -(function(v){if(!Object.keys){var x=!0,y="toString toLocaleString valueOf hasOwnProperty isPrototypeOf propertyIsEnumerable constructor".split(" "),z=y.length,A;for(A in{toString:null})x=!1;Object.keys=function(c){if("object"!==typeof c&&"function"!==typeof c||null===c)throw new TypeError("Object.keys called on a non-object");var e=[],f;for(f in c)c.hasOwnProperty(f)&&e.push(f);if(x)for(f=0;f>>0;if(0===f)return-1;var a=0;0=f)return-1;for(a=0<=a?a:Math.max(f-Math.abs(a),0);ab.weld.classes.indexOf(d[e])){c=!1;break}c&&(p&&t("- REMOVE - element:",l(m),"class:",l(m.className),"id:",l(m.id)),a.removeChild(m))}},traverse:function(a,b,m,d,e){var c,f,g,h=b.parentNode;~{}.toString.call(d).indexOf("Date")&&(d=d.toString());if(d.nodeType||"object"!==typeof d)k.set(a,b,m,d,e);else if(d.length&&d[0])for(h? -k.siblings(h,b,m,d):b.weld&&b.weld.parent&&(h=b.weld.parent),m=d.length,a=0;a>>0;if(0===f)return-1;var a=0;0=f)return-1;for(a=0<=a?a:Math.max(f-Math.abs(a),0);ac.weld.classes.indexOf(d[a])){b=!1;break}b&&(p&&t("- REMOVE - element:",l(m),"class:",l(m.className),"id:",l(m.id)),v.removeChild(m))}},traverse:function(a,c,m,d,e){var b,f,h,g=c.parentNode;~{}.toString.call(d).indexOf("Date")&&(d=d.toString());if(d.nodeType||"object"!==typeof d)k.set(a,c,m,d,e);else if(d.length&&d[0])for(g? +k.siblings(g,c,m,d):c.weld&&c.weld.parent&&(g=c.weld.parent),m=d.length,a=0;acheckbox text text text - - + + +
+
+
+
+
diff --git a/test/test.js b/test/test.js index f67a301..ededed8 100644 --- a/test/test.js +++ b/test/test.js @@ -515,6 +515,31 @@ test.ok($('#a3', template).attr('href') === "https://google.com"); test.ok($('#a3', template).text() === "Google Me"); + test.done(); + }); + }, + "Test 23: Selectively insert objects using the insert-precheck filter": function(test) { + getTemplate('insert-precheck', function(window, weld, $) { + + weld($('.things')[0], + { + things: [ + { thing : '1' }, + { thing : '2' }, + { thing : '3' } + ] + }, + { + insertPrecheck: function(parent, element, key, value) { + // don't insert the second "thing" by returning false + return value.thing != '2'; + } + }); + + test.ok($('.things').length === 2); + test.ok($('.things:nth(0) .thing').text() === '1'); + test.ok($('.things:nth(1) .thing').text() === '3'); + test.done(); }); },