Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(706)

Side by Side Diff: sky/framework/sky-element/TemplateBinding.sky

Issue 812713005: Add declarataive event handlers. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Add tests and fix binding Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/examples/widgets/index.sky ('k') | sky/tests/framework/templates.sky » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <!-- 1 <!--
2 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be 3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file. 4 // found in the LICENSE file.
5 --> 5 -->
6 <import src="observe.sky" as="observe" /> 6 <import src="observe.sky" as="observe" />
7 7
8 <script> 8 <script>
9 Node.prototype.bind = function(name, observable, oneTime) { 9 Node.prototype.bind = function(name, observable, oneTime) {
10 var self = this; 10 var self = this;
11 11
12 if (oneTime) { 12 if (oneTime) {
13 this[name] = observable; 13 this[name] = observable;
14 return; 14 return;
15 } 15 }
16 16
17 observable.open(function(value) { 17 this[name] = observable.open(function(value) {
18 self[name] = value; 18 self[name] = value;
19 }); 19 });
20 20
21 return observable; 21 return observable;
22 }; 22 };
23 23
24 function sanitizeValue(value) { 24 function sanitizeValue(value) {
25 return value == null ? '' : value; 25 return value == null ? '' : value;
26 } 26 }
27 27
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 var iter = node.processBindingDirectives_(bindings); 537 var iter = node.processBindingDirectives_(bindings);
538 if (instanceBindings && iter) 538 if (instanceBindings && iter)
539 instanceBindings.push(iter); 539 instanceBindings.push(iter);
540 } 540 }
541 541
542 function parseWithDefault(el, name, prepareBindingFn) { 542 function parseWithDefault(el, name, prepareBindingFn) {
543 var v = el.getAttribute(name); 543 var v = el.getAttribute(name);
544 return parseMustaches(v == '' ? '{{}}' : v, name, el, prepareBindingFn); 544 return parseMustaches(v == '' ? '{{}}' : v, name, el, prepareBindingFn);
545 } 545 }
546 546
547 function addEventHandler(element, name, method) {
548 element.addEventListener(name, function(event) {
549 var scope = element.ownerScope;
550 var host = scope.host;
551 var handler = host && host[method];
552 if (handler instanceof Function)
553 return handler.call(host, event);
554 });
555 }
556
547 function parseAttributeBindings(element, prepareBindingFn) { 557 function parseAttributeBindings(element, prepareBindingFn) {
548 var bindings = []; 558 var bindings = [];
549 var ifFound = false; 559 var ifFound = false;
550 var bindFound = false; 560 var bindFound = false;
551 var attributes = element.getAttributes(); 561 var attributes = element.getAttributes();
552 562
553 for (var i = 0; i < attributes.length; i++) { 563 for (var i = 0; i < attributes.length; i++) {
554 var attr = attributes[i]; 564 var attr = attributes[i];
555 var name = attr.name; 565 var name = attr.name;
556 var value = attr.value; 566 var value = attr.value;
557 567
558 if (isTemplate(element) && 568 if (isTemplate(element) &&
559 (name === IF || name === BIND || name === REPEAT)) { 569 (name === IF || name === BIND || name === REPEAT)) {
560 continue; 570 continue;
561 } 571 }
562 572
573 if (name.startsWith('on-')) {
574 if (!bindings.eventHandlers)
575 bindings.eventHandlers = new Map();
576 bindings.eventHandlers.set(name.substring(3), value);
577 continue;
578 }
579
563 var tokens = parseMustaches(value, name, element, 580 var tokens = parseMustaches(value, name, element,
564 prepareBindingFn); 581 prepareBindingFn);
565 if (!tokens) 582 if (!tokens)
566 continue; 583 continue;
567 584
568 bindings.push(name, tokens); 585 bindings.push(name, tokens);
569 } 586 }
570 587
571 if (isTemplate(element)) { 588 if (isTemplate(element)) {
572 bindings.isTemplate = true; 589 bindings.isTemplate = true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 instanceBindings); 628 instanceBindings);
612 } 629 }
613 630
614 if (bindings.isTemplate) { 631 if (bindings.isTemplate) {
615 clone.instanceRef_ = node; 632 clone.instanceRef_ = node;
616 633
617 if (delegate) 634 if (delegate)
618 clone.setDelegate_(delegate); 635 clone.setDelegate_(delegate);
619 } 636 }
620 637
638 if (bindings.eventHandlers) {
639 bindings.eventHandlers.forEach(function(handler, eventName) {
640 addEventHandler(clone, eventName, handler);
641 });
642 }
643
621 processBindings(clone, bindings, model, instanceBindings); 644 processBindings(clone, bindings, model, instanceBindings);
622 return clone; 645 return clone;
623 } 646 }
624 647
625 function createInstanceBindingMap(node, prepareBindingFn) { 648 function createInstanceBindingMap(node, prepareBindingFn) {
626 var map = getBindings(node, prepareBindingFn); 649 var map = getBindings(node, prepareBindingFn);
627 map.children = {}; 650 map.children = {};
628 var index = 0; 651 var index = 0;
629 for (var child = node.firstChild; child; child = child.nextSibling) { 652 for (var child = node.firstChild; child; child = child.nextSibling) {
630 map.children[index++] = createInstanceBindingMap(child, prepareBindingFn); 653 map.children[index++] = createInstanceBindingMap(child, prepareBindingFn);
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 this.closeInstanceBindings(this.instances[i]); 1027 this.closeInstanceBindings(this.instances[i]);
1005 } 1028 }
1006 1029
1007 this.instances.length = 0; 1030 this.instances.length = 0;
1008 this.closeDeps(); 1031 this.closeDeps();
1009 this.templateElement_.iterator_ = undefined; 1032 this.templateElement_.iterator_ = undefined;
1010 this.closed = true; 1033 this.closed = true;
1011 } 1034 }
1012 }; 1035 };
1013 </script> 1036 </script>
OLDNEW
« no previous file with comments | « sky/examples/widgets/index.sky ('k') | sky/tests/framework/templates.sky » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698