OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of template_binding; |
| 6 |
| 7 /** |
| 8 * Template Bindings native features enables a wide-range of use cases, |
| 9 * but (by design) don't attempt to implement a wide array of specialized |
| 10 * behaviors. |
| 11 * |
| 12 * Enabling these features is a matter of implementing and registering a |
| 13 * BindingDelegate. A binding delegate is an object which contains one or more |
| 14 * delegation functions which implement specialized behavior. This object is |
| 15 * registered via [TemplateBindExtension.bindingDelegate]: |
| 16 * |
| 17 * HTML: |
| 18 * <template bind> |
| 19 * {{ What!Ever('crazy')->thing^^^I+Want(data) }} |
| 20 * </template> |
| 21 * |
| 22 * Dart: |
| 23 * class MySyntax extends BindingDelegate { |
| 24 * prepareBinding(path, name, node) { |
| 25 * // The magic happens here! |
| 26 * } |
| 27 * } |
| 28 * ... |
| 29 * templateBind(query('template')) |
| 30 * ..bindingDelegate = new MySyntax() |
| 31 * ..model = new MyModel(); |
| 32 * |
| 33 * |
| 34 * See |
| 35 * <http://www.polymer-project.org/platform/template.html#binding-delegate-api> |
| 36 * for more information about the binding delegate. |
| 37 */ |
| 38 // TODO(jmesserly): need better api docs here. The link above seems out of date. |
| 39 class BindingDelegate { |
| 40 /** |
| 41 * Prepares a binding. This is called immediately after parsing a mustache |
| 42 * token with `{{ path }}` in the context of the [node] and the property named |
| 43 * [name]. This should return a function that will be passed the actual |
| 44 * node and model, and either returns null or an object with a `value` |
| 45 * property. This allows the syntax to reinterpret the model for each binding. |
| 46 */ |
| 47 PrepareBindingFunction prepareBinding(String path, String name, Node node) |
| 48 => null; |
| 49 |
| 50 /** |
| 51 * Returns a function that can optionally replace the model that will be |
| 52 * passed to [TemplateBindExtension.createInstance]. This can be used to |
| 53 * implement syntax such as `<template repeat="{{ item in items }}">` by |
| 54 * ensuring that the returned model has the "item" name available. |
| 55 */ |
| 56 PrepareInstanceModelFunction prepareInstanceModel(Element template) => null; |
| 57 |
| 58 /** |
| 59 * Returns a function that will be called whenever the position of an item |
| 60 * inside this template changes. |
| 61 */ |
| 62 PrepareInstancePositionChangedFunction prepareInstancePositionChanged( |
| 63 Element template) => null; |
| 64 |
| 65 Expando<_InstanceBindingMap> _bindingMaps; |
| 66 |
| 67 // TODO(jmesserly): if we have use this everywhere, we can avoid many |
| 68 // delegate != null checks throughout the code, simplifying things. |
| 69 // For now we just use it for _bindingMaps. |
| 70 static final _DEFAULT = new BindingDelegate(); |
| 71 } |
| 72 |
| 73 typedef PrepareBindingFunction(model, Node node, bool oneTime); |
| 74 |
| 75 typedef PrepareInstanceModelFunction(model); |
| 76 |
| 77 typedef PrepareInstancePositionChangedFunction(TemplateInstance instance, |
| 78 int index); |
OLD | NEW |