2

Here's something I thought might be a bit easier. Despite the specifics of the question, I'm interested in any method that will let me have a third form field auto-updated based on the content of two other fields with Polymer.dart.

Something like this, where the "[ ]" represent form fields.

Name: [given name] [family name]

Full name: [family_name, given_name]

So for example; if someone enters "John" and "Smith" in the first two fields. Then the "full name" line shows: [Smith, John], when either of the fields are updated.

I've based the following example on the classes and mark-up from the form Dart Polymer tutorial

For a form like this ...

  <polymer-element name="reference-form" extends="form" >
    <template>
      <style> ... </style>
      <div id="slambookform"  >
        <div class="entry">
          <label>Author:</label>
          <input type="text" value="{{theData['authorGivenName']}}" >
          <input type="text" value="{{theData['authorFamilyName']}}">
        </div>
          :
        <div class="entry">
          <label>Full name:</label>
          <input disabled type="text" value="{{fullName}}" >
        </div>
          :
      </div>
    <template>
  </polymer-element>

My initial attempt to make this happen was a function like:

@observable
String fullName(){

    return theData['authorFamilyName'] +', '+ theData['authorGivenName'];

}

Which doesn't work. When I make 'fullName' to an @observable variable and update it with a button the form is updates as required. Hence my question, can I bind a third field to two (or more) others?

I think I will need some kind of event handler. For two fields, formatting on a change even is simple enough. I want to format several fields in the ultimate case, not just two fields.

While on this topic, is there a hook in dart-polymer or dart to supply a future or call-back? In my example, something like: 'after-change'. Just thinking out loud, something like that would be good.

Thanks in advance.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
will
  • 4,799
  • 8
  • 54
  • 90

3 Answers3

2

Along those lines (caution - code is not tested)

 <polymer-element name="reference-form" extends="form" >
    <template>
      <style> ... </style>
      <div id="slambookform"  >
        <div class="entry">
          <label>Author:</label>
          <input type="text" value="{{authorGivenName}}" >
          <input type="text" value="{{authorFamilyName}}">
        </div>
          :
        <div class="entry">
          <label>Full name:</label>
          <input disabled type="text" value="{{fullName}}" >
        </div>
          :
      </div>
    <template>
  </polymer-element>

class reference_form.dart

String _authorGivenName;
@observable get authorGivenName => _authorGivenName;
set authorGivenName(String val) {
  _authorGivenName = val; 
  notifyPropertyChange(#fullName, '${_authorGivenName} ${_authorFamilyName}', 
    '${val} ${_authorFamilyName}');
}

String _authorFamilyName;
@observable get authorFamilyName => _authorFamilyName;
set authorFamilyName(String val) {
  _authorFamilyName = val; 
  notifyPropertyChange(#fullName, '${_authorGivenName} ${_authorFamilyName}',
    '${_autorGivenName} ${val}');
}

@observable
String get fullName => '${_authorGivenName} ${_authorFamilyName}';
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

I have a workaround for this problem, standing on the shoulders of Günter Zöchbauer (comment above). My objective is to "bind" one field value to two in a read-only fashion. We are not quite there yet, however the pathway is educational in its own right.

Observer method

This solution is kind of a workaround for the objective I set myself. I've made some annotations on this code to explain what I saw, or why I think is happening.

The intention is for fullName to show both names in the form:

  • familyName, givenName; e.g.
  • Smith, John

reference-form.html:

<polymer-element name="reference-form" extends="form" >
    <template>
      <style> ... </style>
      <div id="slambookform"  >
        <div class="entry">
          <label>Author:</label>
          <input type="text" value="{{theData['givenName']}}" >
          <input type="text" value="{{familyName}}">
        </div>
          :
        <div class="entry">
          <label>Full name:</label>
          <input disabled type="text" value="{{fullName}}" >
        </div>
          :
      </div>
    <template>
  </polymer-element>                                          

The code for the form properties, the things Polymer-dart binds to the HTML with the moustache syntax, "{{fullName}}". To keep things simple, I used just one 'notifier' field and this updates the fullName field from both familyName and givenName.

reference_form.dart:

  //---- testing ----

  String _familyName;                        // (1) 
  @observable                                // (2)
  String get familyName => _familyName;      // (3)
  void   set familyName( String nam ){       // (4)
      _familyName = nam; 
      fullName    = notifyPropertyChange(    // (5)
                        #fullName, 
                        "${fullName}", 
                        "${nam}, ${theData['givenName']}" );
  }

  @observable
  String fullName;                           // (6)

  //---- end: testing ----

The private member, "_familyName", is a shadow for the public familyName property used in the template (snippet above).

  1. Shadow (private) member, "_familyName", stores the data for the familyName pseudo property.
  2. The next three lines declare an @observable property, familyName
  3. Get familyName. Simply echo the value for the shadow variable.
  4. Set familyName. Updates the shadow variable and the composite fullName property.
    • Note: the composit formatting could be done with two lines:
      _familyName = nam;
      fullName = nam;
      ... But we want to see all changes propagated see (#5).
  5. The notifyPropertyChange() method updated all observers of the fullName property.
    • Note: I didn't hack around inside Polymer itself; inside the Observable class, fullName doesn't has no observers with the code shown.
    • Until I saw this, I assumed that the Polymer binding to the HTML template was via an observer (watcher), it would seem not. I may be mistaken. In any case, the call to notifyPropertyChange() for the '#fullName' symbol didn't change the results for this test case.
  6. fullName property bound to the Polymer form.

Basically the {{fullName}} value will be updated every time there's a change to the familyName pseudo property.

Note on efficiency:

  • The familyName setter is called with every keystroke (observed while debugging). I understand that, and suggest it is not always really the best solution.
  • For me, I'd prefer to only call the setter when a user exits the field. However when I used onblur, the trigger was a blur of the form, not the field.
  • It seems that we might all benefit in terms of performance with a bit more insider information about these hooks, pathways and any options available to make things more efficient.

Comments and improvements welcome. This example is a workaround for me, so its definitely a work in progress. ;-)

Encapsulation method

I am evolving a solution closer to the original ambition and based on the 'observer method' above. This approach relies on the current, i.e. Dart v4, use of modules and libraries. I'll show the working code first and explain interesting stuff with notes.

reference_form.dart:

import 'package:exportable/exportable.dart';                // [1]

class _Data                                                 // [2]
    extends Object with Exportable {                        // [3]

    @export String publishDate;                             // [4]
    @export String authorGivenName  = '(given)';
    @export String authorFamilyName = '(family)';
    @export String authorUrl        = ''; 

        //--- attributes ---

    String get fullName => "${authorFamilyName}, ${authorGivenName}"; // [5]
    void   set fullName( String nam ){                                // [6]
        //don't need this
    }

        //--- ctor ---

    _Data(){
        publishDate  = new DateTime.now().toString();                 // [7]
    }

} //_Data


@CustomTag('reference-form')
class SlamBookComponent extends FormElement with Polymer, Observable {

  SlamBookComponent.created() : super.created();

  //---- testing ----

  @observable
  _Data data = new _Data();                                           // [8]

      :

} //SlambookComponent      

Notes:

  1. Include Exportable mixin to convert to JSON. I'm not exporting 'fullName' because it is just formatting at the moment.
    • Add exportable to your pubspec.yaml and 'Run Pub get'.
  2. The "_Data" class is private to the reference_form.dart module. I did a bit of testing of the scope rules because I do not want the internal data structure to leak, except for something catholic like JSON of course (small-c).
  3. Bring-in the Exportable mixin.
    • I have tested Exportable, it implements exactly what I thought I'd have to write myself. Happy with this.
    • JSON is not a requirement of the original question; but I did want the (eventual) solution to be a first class artefact that can be serialized or saved is important in the majority of my use-cases.
    • This is a very good example of the facility to extend Dart quick and agile!
  4. Use the @export modifier to identify fields specific to be interchanged as JSON.
  5. Export the fullName attribute as a String (get).
  6. There is no need for set operation. However Dart apparently insists that a Set method matches 'get'.
    • I am disappointed by this. I much prefer the idea that I can have READ-ONLY properties and attributes, e.g. like .
    • As tested, Dart SDK v1.4.0; fails when a matching setter is not implemented/declared(??).
  7. Use a constructor to set initial values for Date data attribute.
  8. Declares an opaque public property called "data", as an (private) _Data instance.
    • The data formatting of key fields is encapsulated in the private _Data declaration.
    • The Exportable mixin interface is used to map the private class to a public JSON result.

Point #8 demonstrates a powerful aspect of , to enable an opaque implementation of objects and yet, you can 'deliver'/'share' details without specific internal details.

I have run this code and checked that the concepts work for hidden data (the _Data type) and opaque access and serialisation. Also you can't accidentally look at internal private type (accidentally, although explicit hacks may be possible). I don't apologise for accepting the C / C++ conscious responsibility paradigm -- I think this a the most powerful aspect of being a programmer; WE are responsible for effects/bugs stemming from the code we produce. I recommend testing 'bits of behaviour' in small mini-use-cases.

I put examples of the markup; nothing surprising. For me this approach is less verbose and a bit more Object Oriented than the original (early) Dart tutorial

reference_form.html

  <polymer-element name="reference-form" extends="form" >
    <template>
    <style> ... </style>
      <div id="slambookform"  >

        <div class="entry">
          <label>Author:</label>
          <input type="text" value="{{data.authorGivenName}}" >
          <input type="text" value="{{data.authorFamilyName}}">
        </div>
        <div class="entry">
          <label>Published:</label>
          <input type="date" value="{{data.publishDate}}">  
        </div>

      </div>
    </template>

    <script type="application/dart" src="reference_form.dart"> </script>
  </polymer-element>

In the Polymer mark-up can know (and has visibility over) internal field names. Why?

... Because the "reference_form.html" and "reference_form.dart" via Polymer-dart. It is quite nice really; although it seems that the ".dart" and ".html" components are closely coupled like ASP.NET and C#/VN.NET as (also) specified by convenience(??). I confess that's a completely different subject; there are things to resolve to keep things yar (yachting term).

Anyway for me, I feel the approach begun with the encapsulation shamble above is better suited to my needs for a small utility.


will
  • 4,799
  • 8
  • 54
  • 90
  • Can you create a question for you onBlur problem? – Günter Zöchbauer Apr 26 '14 at 12:02
  • I disagree regarding the comment to use the _*notifyPropertyChange*()_ return. It is needed imho, to ensure that the fullName property is given the same value as any down-stream dependant observers. It's a _data integrity_ issue. In threaded code and message passing systems like Ruby, Smalltalk and MCS-s, bugs happen when an upstream element assumes the data passed-on will be the same-as the result. In the message environment, you must never assume that. It's a hot-list item for 'message systems' (MCS) like MVC. That said; yes you can _cheat_ with it on a small app like this use case. – will Apr 26 '14 at 12:23
  • Ok, but in my code the return value was assigned to the wrong field. It's the value for the change-notified field not the value of the backing field of the current setter. – Günter Zöchbauer Apr 26 '14 at 12:27
  • @Günter ... Backing field comment. I debugged that; it didn't work that way with Dart v1.4. I added the more explicit, verbose setter syntax to unwrap any Dart stuff, but it wouldn't work. That's when I went to [Dart tips - Getters/Setters](http://www.dartlang.org/dart-tips/dart-tips-ep-10.html) shown above. Anyway the answer above is just a prototype. I definitely want to find a way to bind elements because I want to _blend_ many fields in different ways. – will Apr 26 '14 at 12:34
1

Polymer now supports this use case directly with @ObserveProperty

  @observable String authorGivenName = '';
  @observable String authorFamilyName = '';
  @observable String get fullName => '${authorGivenName} ${authorFamilyName}';

  @ObserveProperty('authorGivenName authorFamilyName')
  void updateFullName(old) {
    notifyPropertyChange(#fullName, old, fullName);
  }
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567