Files
sdk/pkg/observe/lib/html.dart
T
jmesserly@google.com 38c4cd1f6b package:observe fix various api issues
in particular:
provide oldValue/newValue/target in PropertyChangeRecord -- https://code.google.com/p/dart/issues/detail?id=12075
use == in notifyPropertyChange -- https://code.google.com/p/dart/issues/detail?id=12463
remove Mixin/Base suffix -- https://code.google.com/p/dart/issues/detail?id=13353
remove ChangeRecord.changes -- https://code.google.com/p/dart/issues/detail?id=14128

other changes:
rename PropertyChangeRecord.field to .name (it's not just fields)
PathObserver rethrows errors from inside the getter/setter to help debug those
PathObserver now looks up Map keys using Strings instead of Symbols.
generic type on PropertyChangeRecord, MapChangeRecord

I stopped short of adding "target" to List/Map change record but am interested in a follow up to address that: https://code.google.com/p/dart/issues/detail?id=14157

R=sigmund@google.com

Review URL: https://codereview.chromium.org//27618002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@28774 260f80e4-7a28-3924-810f-c04153c831b5
2013-10-17 03:32:40 +00:00

71 lines
2.2 KiB
Dart

// 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 more elegantly?
// In general, it seems like we want a convenient way to take a Stream plus a
// getter and convert this into an Observable.
/** Helpers for exposing dart:html as observable data. */
library observe.html;
import 'dart:html';
import 'observe.dart';
/** An observable version of [window.location.hash]. */
final ObservableLocationHash windowLocation = new ObservableLocationHash._();
class ObservableLocationHash extends ChangeNotifier {
Object _currentHash;
ObservableLocationHash._() {
// listen on changes to #hash in the URL
// Note: listen on both popState and hashChange, because IE9 doesn't support
// history API. See http://dartbug.com/5483
// TODO(jmesserly): only listen to these if someone is listening to our
// changes.
window.onHashChange.listen(_notifyHashChange);
window.onPopState.listen(_notifyHashChange);
_currentHash = hash;
}
@reflectable String get hash => window.location.hash;
/**
* Pushes a new URL state, similar to the affect of clicking a link.
* Has no effect if the [value] already equals [window.location.hash].
*/
@reflectable void set hash(String value) {
if (value == hash) return;
window.history.pushState(null, '', value);
_notifyHashChange(null);
}
void _notifyHashChange(_) {
var oldValue = _currentHash;
_currentHash = hash;
notifyPropertyChange(#hash, oldValue, _currentHash);
}
}
/** Add or remove CSS class [className] based on the [value]. */
void updateCssClass(Element element, String className, bool value) {
if (value == true) {
element.classes.add(className);
} else {
element.classes.remove(className);
}
}
/** Bind a CSS class to the observable [object] and property [path]. */
PathObserver bindCssClass(Element element, String className,
Observable object, String path) {
return new PathObserver(object, path)..bindSync((value) {
updateCssClass(element, className, value);
});
}