I am looking for documentation, but I don't know what term I should be searching with.
I have an observable:
#observable String inputName66='';
and (from various cuts and pastes from other users' codes!), this function:
void inputName66Changed(Event e, var newValue, var Target) {
So the "inputName66Changed" is an "expected" function, expected by Dart (Polymer?) by extending the observable name by adding "Changed".
Where can I find the documentation for these "name extensions" please? And what other extensions are available? I assume they're generated by the Observe class, but I don't see it.
Thanks in advance
Steve
This is one of the Polymer lifeCycle methods. See https://www.polymer-project.org/docs/polymer/polymer.html#lifecyclemethods (attributeChanged)
Related
I would like to understand how built in methods work in Dart, therefore i would like to access the source code, any idea how to access a source code of built in methods? below is an example of one method map()
enter image description here
Well, it can be a little complicated to find the concrete implementation of all methods. There are several reasons for that:
Some implementations are different for Dart running as JavaScript and Dart running on the Dart VM.
Some methods are heavily dependent on C++ code for the Dart VM implementation.
Lots of classes in Dart are just interfaces like the Iterable which means the Iterable.map method can have different implementations for e.g. lists and maps.
However, all of the source code can be found in the GitHub repository if you browse through the code. But it takes some experience to know where to look for things:
https://github.com/dart-lang/sdk/tree/stable
So if we e.g. want to look for the default map method in Iterable we need to see if there are a default implementation. We can find the class here in the repo.:
https://github.com/dart-lang/sdk/blob/9a618e5661665b8d687a28e6b1ec25e9177ec2d7/sdk/lib/core/iterable.dart#L197
Iterable<T> map<T>(T f(E e)) => MappedIterable<E, T>(this, f);
By searching for class MappedIterable we can see this class is implemented in:
https://github.com/dart-lang/sdk/blob/stable/sdk/lib/internal/iterable.dart#L354
class MappedIterable<S, T> extends Iterable<T> {
final Iterable<S> _iterable;
final _Transformation<S, T> _f;
factory MappedIterable(Iterable<S> iterable, T function(S value)) {
if (iterable is EfficientLengthIterable) {
return new EfficientLengthMappedIterable<S, T>(iterable, function);
}
return new MappedIterable<S, T>._(iterable, function);
}
MappedIterable._(this._iterable, this._f);
Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f);
// Length related functions are independent of the mapping.
int get length => _iterable.length;
bool get isEmpty => _iterable.isEmpty;
// Index based lookup can be done before transforming.
T get first => _f(_iterable.first);
T get last => _f(_iterable.last);
T get single => _f(_iterable.single);
T elementAt(int index) => _f(_iterable.elementAt(index));
}
One details you will see is that a lot of the SDK are duplicated right now in a sdk and sdk_nnbd version. This is temporarily while the Dart team are implementing the non-nullable by default feature and is not used unless you are running with this feature turned on.
With dart it is possible to prefix class/function definition by a commentary so that the analyzer will interpret it as documentation:
/// Some documentation for [Foo]
class Foo {
}
But how can I achieve the same behavior, with parameters instead?
I tried the following:
void myFunction(
/// Some parameter documentation
String parameter,
) {
}
But this doesn't work.
However, it seems possible because dartanalyzer do contain a property on ParameterElement for documentation.
https://www.dartlang.org/guides/language/effective-dart/documentation
Here's the offical Dart language guidelines for documentation best practice. It covers most/all cases and has great examples of do's and don't's.
This bit shows the way to include parameters. Basically, wrap the parameters in square brackets and within a sentence explaining it.
I am new to dart, and I am not familiar with this concept. Some patience is appreciated.
I was reading some code, here, when I came across this. (line 14)
static final none = Motility._(0);
Looking at the second half of the assignment, I can see a private function that takes an integer, but after some searching I do not see a definition in the class.
So, my question is what is this mysterious function? I am assuming this is a feature of the language, but I am having trouble looking it up since I have never heard of this concept!
It invokes the constructor
Motility._(this._bitMask);
https://github.com/munificent/hauberk/blob/master/lib/src/engine/stage/tile.dart#L28
It is not that obvious anymore since new became optional, but it is a common pattern to have private constructors. (Identifiers starting with _ are private in Dart)
Motility is basically an enum that is built this way instead of
enum Motility { none, door, fly, swim, walk, doorAndFly, doorAndWalk, flyAndWalk }
because this way custom values can be assigned.
In some cases, I would like to add contextual information to a message (for instance currently authenticated user), without having to include it in the message template.
I would like to accomplish this :
logger.Information("Doing stuff {Foo} with the thing {Bar}. {User}", foo, bar, user)
but without the {User} in the template.
I already know about LogContext but that seems overkill when adding contextual information to just one event.
I also know I can use the low-level API logger.Write(LogEvent evnt) to actually control which properties are included, but that seems like a bit too much code for what I am trying to accomplish.
I'm pretty sure there is a short and elegant way that is super obvious, but I haven't found it :)
UPDATE :
I found out only afterwards that this question is more or less similar : Add custom properties to Serilog
I could figure it out on my own!
You can use the fluent method .ForContext(propertyName, propertyValue) on a single call to one of the .LogXXX() methods.
For instance :
logger.ForContext("User", user)
.Information("Doing stuff {Foo} with the thing {Bar}", foo, bar)
The property added to the event only apply to the event, and they are no longer present on the next call to the logger.LogXXX() method
UPDATE
The article by Nicholas Blumhardt explains it quite well : Context and correlation – structured logging concepts in .NET (5)
If you're using the generic Microsoft ILogger you can use BeginScope;
using (_logger.BeginScope(new Dictionary<string, object> { { "LogEventType", logEventType }, { "UserName", userName } }))
{
_logger.LogInformation(message, args);
}
This is discussed here; https://blog.rsuter.com/logging-with-ilogger-recommendations-and-best-practices/
Once again, a Dart/Polymer related question.
I wanted to use the Parse.com JavaScript library, but since it's not available in Dart I've written Wrapper classes which store a JsObject and delegate all calls from Dart to the corresponding JavaScript object. Basically it's like a proxy.
Guess what, it works pretty great.
However, my observables don't. To understand this, you have to take a look at the following structure of one of my "proxy"-classes.
class ParseObject extends Observable {
JsObject _jsDelegate = new JsObject(context['Parse']['ParseObject']);
void set(String key, dynamic value) {
_jsDelegate.callMethod('set', [key, jsify(value)];
}
dynamic get(String key) {
return dartify(_jsDelegate.callMethod('get', [key]));
}
}
The HTML code of my Polymer Element looks like this:
<div>Name: {{project.get('name')}}</div>
Since the data binding is only evaluate in case the parameter of the method changed, it will never be updated and thus even though the name is changed, the old one will stay in place.
The solution I came up with is to store all the values the user is setting in the ParseObject#set(String, dynamic) method into a Map which is observable. This works but I think it's quiete dirty since I have to make sure that both Maps, the one in Dart and the one in the ParseObject's JavaScript representation equal.
Thus I am looking for a better solution and I think of some kind of method to tell Polymer to reevaluate it's data bindings.
Does such a method exist or are there any other possibilities to address this problem?
Extending observable by itself does nothing yet.
You need to annotate the getters with #observable (and if you are not using Polymer, you also need to add the observable transformer to pubspec.yaml). You can't make functions observable (this works in Polymer elements but not in Observable model classes. For more details about observable see for example Implement an Observer pattern in Dart or Dart: #observable of getters