OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 library polymer.src.common.polymer_serialize; | 4 library polymer.src.common.polymer_serialize; |
5 | 5 |
6 import 'dart:js'; | 6 import 'dart:js'; |
7 import 'js_proxy.dart'; | 7 import 'js_proxy.dart'; |
8 import 'polymer_mixin.dart'; | 8 import 'polymer_mixin.dart'; |
9 import 'polymer_descriptor.dart'; | 9 import 'polymer_descriptor.dart'; |
10 | 10 |
11 /// Mixin for Polymer serialization methods. | 11 /// Mixin for Polymer serialization methods. |
12 /// | 12 /// |
13 /// This should only be used if the [serialize] and [deserialize] methods need | 13 /// This should only be used if the [serialize] and [deserialize] methods need |
14 /// to be overridden to support additional Dart types. Any types not explicitly | 14 /// to be overridden to support additional Dart types. Any types not explicitly |
15 /// handled by the overridden method should defer to the original method by | 15 /// handled by the overridden method should defer to the original method by |
16 /// calling the base class's implementation. | 16 /// calling the base class's implementation. |
17 abstract class PolymerSerialize implements PolymerMixin { | 17 abstract class PolymerSerialize implements PolymerMixin { |
18 JsObject get jsElement; | 18 JsObject get jsElement; |
19 | 19 |
20 /// Serializes the [value] into a [String]. | 20 /// Serializes the [value] into a [String]. |
21 String serialize(Object value) { | 21 String serialize(value) { |
22 var result = jsElement.callMethod('originalSerialize', [jsValue(value)]); | 22 var result = _polymerDartSerialize.apply([jsValue(value)]); |
23 | 23 |
24 return (result != null) ? result.toString() : null; | 24 return (result != null) ? result.toString() : null; |
25 } | 25 } |
26 | 26 |
27 /// Deserializes the [value] into an object of the given [type]. | 27 /// Deserializes the [value] into an object of the given [type]. |
28 dynamic deserialize(String value, Type type) { | 28 dynamic deserialize(String value, Type type) { |
29 return dartValue(jsElement.callMethod( | 29 return dartValue(_polymerDartDeserialize.apply([value, jsType(type)])); |
30 'originalDeserialize', [jsValue(value), jsType(type)])); | |
31 } | 30 } |
32 } | 31 } |
| 32 |
| 33 final JsObject _polymer = context['Polymer']; |
| 34 final JsFunction _polymerDartSerialize = _polymer['Dart']['serialize']; |
| 35 final JsFunction _polymerDartDeserialize = _polymer['Dart']['deserialize']; |
OLD | NEW |