Chromium Code Reviews| Index: lib/observe/html.dart |
| diff --git a/lib/observe/html.dart b/lib/observe/html.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..888ee242d61439f91a5a4a36661e398bf5b2b45c |
| --- /dev/null |
| +++ b/lib/observe/html.dart |
| @@ -0,0 +1,43 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// TODO(jmesserly): can we handle this better? This seems like an unforunate |
|
Siggi Cherem (dart-lang)
2013/02/13 01:43:24
nit: unforunate -> unfor[t]unate
Jennifer Messerly
2013/02/13 05:43:15
What an unforunate typo!
|
| +// limitation of our read barriers. |
|
Siggi Cherem (dart-lang)
2013/02/13 01:43:24
I guess there is the general problem of handling n
Jennifer Messerly
2013/02/13 05:43:15
yeah, I dunno. Mostly we aren't observing the DOM
|
| + |
| +/** Helpers for exposing dart:html as observable data. */ |
| +library web_ui.observe.html; |
| + |
| +import 'dart:html'; |
| +import 'package:web_ui/observe.dart'; |
| + |
| +ObservableReference<String> _hash; |
| + |
| +/** An observable version of [window.location.hash]. */ |
| +String get locationHash { |
| + if (_hash == null) { |
| + _hash = new ObservableReference(window.location.hash); |
| + |
| + window.onHashChange.listen(_updateLocationHash); |
| + window.onPopState.listen(_updateLocationHash); |
| + } |
| + |
| + return _hash.value; |
| +} |
| + |
| +/** Pushes a new URL state, similar to the affect of clicking a link. */ |
| +set locationHash(String value) { |
| + window.history.pushState(null, '', value); |
| + _updateLocationHash(null); |
| +} |
| + |
| + |
| +// listen on changes to #hash in the URL |
| +// Note: listen on both popState and hashChange, because IE9 doesnh't support |
| +// history API, and it doesn't work properly on Opera 12. |
| +// See http://dartbug.com/5483 |
| +_updateLocationHash(_) { |
| + if (_hash != null) { |
| + _hash.value = window.location.hash; |
| + } |
| +} |