0

I am having some difficulty conceptualizing how to access a specific CustomEvent.

Here is my issue.

I have a name-form.html that I import in nok.html and pt.html.

name-form.html has a custom event that publishes the observable model for the form. This published model is then subscribed to by the detail parameter of the CustomEvent. The problem is although I have three different forms to capture a name each with a different context, name-form.html will fire the same custom event.

My thoughts are that I will have to duplicate the nok-form.html 3 times and change the custom-event name to listen to it it the appropriate context. Although this works, you can see how the code gets bloated for each duplicated file, just to change the published event.

Any ideas how I could accomplished this otherwise would be appreciated.

Thanks

edit

This is just to add the missing code.

nok-form.html

    <!DOCTYPE html>

    <polymer-element name='name-form'>
      <template>
        <form>
         <section id='first'>
           <label for='firstTxt' >First</label>

            <input id='first-name'
             name='first-name'
             type="text"
             value="{{name.first}}"
             required
             on-change='{{submit}}'>
          </section>

            <button id='submit-btn'
              type='submit'></button>
          </form>
       </template>

       <script type="application/dart">

        import 'package:polymer/polymer.dart';
        import 'dart:html';



        @CustomTag( 'name-form' )
        class NameForm extends PolymerElement
        {

          @observable Name  name = new Name();

          NameForm.created() : super.created();

          void submit ( Event e, var detail, Node target )
          {


            $[ 'submit-btn' ].click();

            $['form'].onSubmit
              .listen( ( e )
                  {
                    dispatchEvent(new CustomEvent('nameEvent', detail:name ) );
                    e.preventDefault();
                  });
          }
        }

      </script>
    </polymer-element>

nok-form.html

  <!DOCTYPE html>

  <link rel="import" href="name-form.html">

  <polymer-element name='nok-form'>
    <template>

      <name-form id='name-form' on-nameEvent={{useModel}}></name-form>


     </template>

     <script type="application/dart">

      import 'package:polymer/polymer.dart';
      import 'dart:html';


      @CustomTag( 'nok-form' )
      class NokForm extends PolymerElement
      {


        NokForm.created() : super.created();

        void useModel ( CustomEvent e, var detail, Node target )
        {
          // This output will be the same as for patient-form
          // since the event detail is the same
          print ( e.detail.fistname );
        }

      }

    </script>
  </polymer-element>

patient-form.html

  <!DOCTYPE html>

  <link rel="import" href="name-form.html">

  <polymer-element name='patient-form'>
    <template>

      <name-form id='name-form' on-nameEvent={{useModel}}></name-form>


     </template>

     <script type="application/dart">

      import 'package:polymer/polymer.dart';
      import 'dart:html';


      @CustomTag( 'patient-form' )
      class PatientForm extends PolymerElement
      {


        PatientForm.created() : super.created();

        void useModel ( CustomEvent e, var detail, Node target )
        {
          // This output will be the same as for nok-form
          // since the event detail is the same
          print ( e.detail.fistname );
        }

      }

    </script>
  </polymer-element>


 class Name
 {
    String firstname = '';

  }

Question: How can I get the name-form.html to send the CustomEvent to either nok-form.html or patient-form.html. Currently the same event is sent to both. As you can see the data collected by the name-form reprsents two different names.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
st_clair_clarke
  • 5,453
  • 13
  • 49
  • 75
  • I think you should provide some code to demonstrate what you are trying to do. – Günter Zöchbauer Jan 02 '14 at 21:00
  • I have still trouble getting what you actually try to achieve. If you import `name-form` in two different places you have two instances of it. Each`s event will be propagated to it's parent which is the element where it is imported to. Please provide some additional information what was your idea of this architecture. – Günter Zöchbauer Jan 03 '14 at 06:39
  • Thank you Gunter. I understand what you said about the propogation. If I was using the event in the parent it would be fine, but I am trying to use CustomEvent to send the nok-form model where I could then listen for it, and use it. When I send the event in both patient-form and name-form, the event detail is the same. If I am not explaining myself properly for that I apologise. If you are aware of how I could accomplish retrieving the model from name-form and send it to its parent without using what I am trying to use, I would be greatful for the information. – st_clair_clarke Jan 03 '14 at 16:51
  • You have a `nok-form` with a child `name-form` and a `patient-form` with another `name-form` as child. When the `name-form` fires an event then it's parent gets the event from it's own child. I just don't understand what you try to achieve. What exactly is this all for? – Günter Zöchbauer Jan 03 '14 at 17:01
  • What I presented was a simplified version of what I want to achieve. I have forms that are nested upto 4 layers deep. The principle of the event bubbling up to its parents still holds irrespective of the number of nested layers? – st_clair_clarke Jan 03 '14 at 17:52
  • Your code should have shown this. – Günter Zöchbauer Jan 03 '14 at 22:38

1 Answers1

0

I think you should set an attribute in markup of the Element and send this value with the event details. This way the receiver receives meta data that helps to decide if he should process the event.

like

<name-form id='name-form' fieldname="firstname" on-nameEvent={{useModel}}></name-form>
<name-form id='name-form' fieldname="lastname" on-nameEvent={{useModel}}></name-form>

or

<name-form id='name-form' reciever="nok-form" on-nameEvent={{useModel}}></name-form>
<name-form id='name-form' reciever="patient-form" on-nameEvent={{useModel}}></name-form>
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567