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

Unified Diff: lib/observe/html.dart

Issue 12225039: Support for observable models, fixes #259 (Closed) Base URL: https://github.com/dart-lang/web-ui.git@master
Patch Set: Created 7 years, 10 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/observe.dart ('k') | lib/observe/list.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/observe/html.dart
diff --git a/lib/observe/html.dart b/lib/observe/html.dart
new file mode 100644
index 0000000000000000000000000000000000000000..544b01b40ae04c6e4d31c820f1cd989f80f79fed
--- /dev/null
+++ b/lib/observe/html.dart
@@ -0,0 +1,48 @@
+// 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 unfortunate
+// limitation of our read barriers.
+
+/** 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.
+ * Has no effect if the [value] already equals [window.location.hash].
+ */
+set locationHash(String value) {
+ if (value == window.location.hash) return;
+
+ window.history.pushState(const {}, '', 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;
+ }
+}
« no previous file with comments | « lib/observe.dart ('k') | lib/observe/list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698