im looking for a way to add an PropertyChangeEvent to an object that I have defined. The goal is to raise a change event when any of the property of the object is been changed.
so i can do something like the following
var newItem:MyObject = new MyObject();
newItem.addEventListener(event.PropertyChangeEvent, myO_PropertyChangeHandler);
class MyObject extends EventDispatcher
{
public function doSomething() :void
{
// change values, and dispatch event
dispatchEvent( PropertyChangeEvent.createUpdateEvent( this, "myProperty", oldValue, newValue ) );
}
}
If you can't extend EventDispatcher because your object extends something else, and if that super class isn't already a subtype of EventDispatcher or implements IEventDispatcher (which includes most types), you need to implement IEventDispatcher manually. See the help page for IEventDispatcher for example code on how you do that (i.e. with an internal EventDispatcher doing the actual job).
If I understand you correctly you're looking for the Bindable Meta tag.
Related
I have a class which should be created during the injection phase but this instance will not be referenced anywhere in the code. The way this class communicates with the others is via event bus
public class DefaultCounterTracker {
private final EventBus eventBus;
private final ReplicatedMap<String, String> trackerCache;
#Inject
public DefaultCounterTracker(
EventBus eventBus,
#Named("CountersTrackerCache") ReplicatedMap<String, String> trackerCache)
{
this.eventBus = eventBus;
this.trackerCache = trackerCache;
bindListeners();
}
private void bindListeners()
{
eventBus.localConsumer(CounterCreated.name(), (Handler<Message<String>>) event ->
{
handleCreation(event.body());
});
eventBus.localConsumer(CounterDestroyed.name(), (Handler<Message<String>>) event ->
{
handleDestruction(event.body());
});
}
Debugging I have seen that this class is created only when somewhere in the code there is written
#Inject
DefaultCounterTracker counterTracker
I have tried binding using a provider but nothing changes: it seems that if the class is not referenced Guice won't create it. Is there a way to tell Guice to do it? Did I miss the point?
I could also create the instance manually but if I do change in default implementation of one of the parameters (EventBus for instance) I need to remember also to change the call in the constructor.
Any suggestion appreciated
If the mentioned class is supposed to be a Singleton, then you can create an object of this class eagerly by specifying a binding in your configure() method of the AbstractModule class.
bind(DefaultCounterTracker.class).to(DefaultCounterTracker.class).asEagerSingleton();
The following example is taken from the polymer-dart documentation on behaviors. It makes use of the method set in toggleHighlight. I don't understand how this is possible since set isn't defined anywhere.
#behavior
abstract class HighlightBehavior {
#Property(notify: true, observer: 'highlightChanged')
bool isHighlighted = false;
static created(instance) {
print('Highlighting for $instance enabled!');
}
#Listen('click')
toggleHighlight(_, __) {
set('isHighlighted', !isHighlighted);
},
#reflectable
highlightChanged(bool newValue, _) {
toggleClass('highlighted', newValue);
}
}
How do I set a polymer property in a behavior that triggers all the functionality that makes data binding work?
Should a behavior possibly implement PolymerBase to be able to use the set-method? A quick test reveals that set works when the behavior implements PolymerBase. But this is not how it is documented. May I induce some unwanted side-effects by implementing PolymerBase?
The HighlightBehavior is abstract, so real instances are obtained with inheritance. From the documentation
class MyElement extends PolymerElement with HighlightBehavior {
MyElement.created() : super.created();
}
The PolymerElement extends PolymerBase which supply the set method.
I have an abstract superclass with a factory that returns an instance of a subclass. Is it possible to have a method that is implemented only in superclass? In the following code, for instance, would it be possible to remove Wind::act()?
abstract class Element {
final String action; // what it does
String act() => action; // do it
factory Element() {
return new Wind();
}
}
class Wind implements Element {
final action = "blows";
act() => action; // Why is this necessary?
}
void main() {
print(new Element().act());
}
When removing Wind::act(), there is an error about it missing. Also, when extending rather than implementing the superclass, leaving out the subclass implementation doesn't cause an error. But with a factory method, extending is not an option.
To inherit functionality from Element in Wind, you need to either extend or mix-in Element in Wind. Merely implementing an interface will not inherit any implementation.
So, you need to have class Wind extends Element { ... }.
That's not currently possible because Element has no generative constructor that Wind can use as super-constructor. So, you need to add that too, and make sure to initialize the action field in that constructor.
class Element {
final String action;
Element._(this.action); // Generative constructor that Wind can use.
factory Element() = Wind; // Factory constructor creating a Wind.
String act() => action;
}
class Wind extends Element {
Wind() : super._("blows");
}
The generative constructor doesn't need to be private, but if you are declaring and using all the classes only inside your own library, it might as well be.
Another option is to have a separate ElementBase class containing the action field and act function and an empty-named generative constructor. Mixins are not a good choice in this case, because there is no good way to make action final when mixins can't have constructors.
abstract class Element {
String get action;
factory Element() = Wind;
String act();
}
class ElementBase implements Element {
final String action;
ElementBase(this.action);
String act() => action;
}
class Wind extends ElementBase {
Wind() : super("blow");
}
It's a common problem to want both a generative constructor for subclasses and a factory constructor generating the default implementation in an interface/skeleton class. The List and Map interfaces have this problem, and have solved it by exposing ListBase and MapBase. I think that is the best solution when you are exposing the superclass to other users in other libraries. If it's only used internally by yourself, I'll use the private/non-default-named generative constructor in the superclass.
I begin with Dart and I would like to extend RectElement class to create a MyRectElement class which is able to move rectangle in SVG area :
import 'dart:html';
import 'dart:svg';
class MyRectElement extends RectElement{
int xOrigin;
int yOrigin;
factory MyRectElement() {
}
}
void main() {
var rect = new MyRectElement();
var container = querySelector("#container");
container.append(rect);
}
But RectElement has a factory constructor.
I must admit that I don't understand factory constructor even if I read lots of posts about it...
What should I put in MyRectElement factory contructor ?
Extending just the class is not supported.
You can build a Polymer element that extends a DOM element or if you don't want to use Polymer this question should provide some information Is it possible to create a Polymer element without Html?
I would like to implement an observer pattern in Dart but I'm not sure how to go about it.
Let's say I have a class:
class MyClass {
String observed_field;
}
Now, whenever I change the field, I'd like to print "observed_field changed" string into the console. Pretty simple to do with a custom setter:
class MyClass {
String _observed_field;
get observed_field => _observed_field;
set observed_field(v) {
_observed_field = v;
print("observed_field changed");
}
}
Now, of course, if I have not one, but many of those fields, I wouldn't want to create all those getters and setters. The obvious theoretical solution is to have them dynamically added to the class with something like this (not a working code, just an example of how I wish it looked):
class MyClass
String _observeable_field;
String _observeable_field_2;
observe(#observeable_field, #observeable_field_2);
end
Is it even possible? Additionally, it would be super awesome to not have those fields defined above the observe() call, but rather write something like:
observe(String: #_observeable_field, String: #_observeable_field_2);
So that those fields are declared automatically.
Here's a way to do it using the Observe package. The example is taken from code comments in that package (and adapted to your example above). Essentially, you annotate fields you want to be observable with the #observable annotation, and then listen for changes (which you trigger with the call to Observable.dirtyCheck();
First, add the observable package in your pubspec.yaml
dependencies:
observe: any
Then create a quick test program...
import 'package:observe/observe.dart';
class MyClass extends Object with Observable {
#observable String observedField = "Hello";
toString() => observedField.toString();
}
main() {
var obj = new MyClass();
// anonymous function that executes when there are changes
obj.changes.listen((records) {
print('Changes to $obj were: $records');
});
obj.observedField = "Hello World";
// No changes are delivered until we check for them
Observable.dirtyCheck();
print('done!');
}
This produces the following output:
Changes to Hello World were: [#<PropertyChangeRecord Symbol("observedField") from: Hello to: Hello World>]
done!
Update in response to comments...
Updating the example to omit the Observable.dirtyCheck() you can use a setter and notifyPropertyChanged, with the class instead mixing in ChangeNotifier
class MyClass2 extends Object with ChangeNotifier {
String _observedField = "Hello";
#reflectable get observedField => _observedField;
#reflectable set observedField(v) {
_observedField = notifyPropertyChange(#observedField, _observedField, v);
}
toString() => observedField;
}