Skip to content

Commit

Permalink
better m-show implementation (perf boost)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Mar 25, 2017
1 parent e63d63e commit 718ff7b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
13 changes: 7 additions & 6 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,12 @@
}
};

specialDirectives[Moon.config.prefix + "show"] = {
duringPropGenerate: function (value, meta, vnode) {
return '"m-show": ' + compileTemplate(value, false) + ', ';
}
};

specialDirectives[Moon.config.prefix + "for"] = {
afterGenerate: function (value, meta, code, vnode) {
var parts = value.split(" in ");
Expand Down Expand Up @@ -1658,12 +1664,7 @@
};

directives[Moon.config.prefix + "show"] = function (el, val, vnode) {
var evaluated = new Function("return " + val);
if (!evaluated()) {
el.style.display = 'none';
} else {
el.style.display = 'block';
}
el.style.display = val ? '' : 'none';
};

directives[Moon.config.prefix + "mask"] = function (el, val, vnode) {};
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions src/directives/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ specialDirectives[Moon.config.prefix + "if"] = {
}
}

specialDirectives[Moon.config.prefix + "show"] = {
duringPropGenerate: function(value, meta, vnode) {
return `"m-show": ${compileTemplate(value, false)}, `;
}
}

specialDirectives[Moon.config.prefix + "for"] = {
afterGenerate: function(value, meta, code, vnode) {
var parts = value.split(" in ");
Expand Down Expand Up @@ -134,12 +140,7 @@ specialDirectives[Moon.config.prefix + "text"] = {
}

directives[Moon.config.prefix + "show"] = function(el, val, vnode) {
var evaluated = new Function("return " + val);
if(!evaluated()) {
el.style.display = 'none';
} else {
el.style.display = 'block';
}
el.style.display = (val ? '' : 'none');
}

directives[Moon.config.prefix + "mask"] = function(el, val, vnode) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ describe("Directive", function() {
}
});
it('should display when true', function() {
expect(document.getElementById('show-condition').style.display).to.equal('block');
expect(document.getElementById('show-condition').style.display).to.equal('');
});
it('should not display when false', function() {
showApp.set('condition', false);
Expand Down

0 comments on commit 718ff7b

Please sign in to comment.