Skip to content

Commit

Permalink
optimize events and create bind event
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jun 28, 2018
1 parent b7fc498 commit 871e598
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
20 changes: 16 additions & 4 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
if (name === undefined || globals.indexOf(name) !== -1) {
return match;
} else {
dynamic = true;

if (name[0] === "$") {
return ("locals." + name);
} else {
dynamic = true;
return ("instance." + name);
}
}
Expand Down Expand Up @@ -481,9 +482,20 @@
var attributeCode = (void 0);

if (attribute.key[0] === "@") {
var eventHandler = root.nextElement++;
createCode$1 += addEventListener(element.element, attribute.key.substring(1), ("function($event){" + (getElement(eventHandler)) + "($event);}"));
attributeCode = setElement(eventHandler, ("function($event){locals.$event=$event;" + (attributeValue(attribute)) + ";};"));
var eventType = (void 0), eventHandler = (void 0);

if (attribute.key === "@bind") {
var bindVariable = attributeValue(attribute);
attributeCode = (getElement(element.element)) + ".value=" + bindVariable + ";";
eventType = "input";
eventHandler = bindVariable + "=$event.target.value;instance.update();";
} else {
attributeCode = "";
eventType = attribute.key.substring(1);
eventHandler = "locals.$event=$event;" + (attributeValue(attribute)) + ";";
}

createCode$1 += addEventListener(element.element, eventType, ("function($event){" + eventHandler + "}"));
} else {
attributeCode = setAttribute(element.element, attribute);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/compiler/generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,20 @@ export const generateAll = (element, parent, root, insert) => {
let attributeCode;

if (attribute.key[0] === "@") {
const eventHandler = root.nextElement++;
createCode += addEventListener(element.element, attribute.key.substring(1), `function($event){${getElement(eventHandler)}($event);}`);
attributeCode = setElement(eventHandler, `function($event){locals.$event=$event;${attributeValue(attribute)};};`);
let eventType, eventHandler;

if (attribute.key === "@bind") {
const bindVariable = attributeValue(attribute);
attributeCode = `${getElement(element.element)}.value=${bindVariable};`;
eventType = "input";
eventHandler = `${bindVariable}=$event.target.value;instance.update();`;
} else {
attributeCode = "";
eventType = attribute.key.substring(1);
eventHandler = `locals.$event=$event;${attributeValue(attribute)};`;
}

createCode += addEventListener(element.element, eventType, `function($event){${eventHandler}}`);
} else {
attributeCode = setAttribute(element.element, attribute);
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/parser/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export const parseTemplate = (expression) => {
if (name === undefined || globals.indexOf(name) !== -1) {
return match;
} else {
dynamic = true;

if (name[0] === "$") {
return `locals.${name}`;
} else {
dynamic = true;
return `instance.${name}`;
}
}
Expand Down

0 comments on commit 871e598

Please sign in to comment.