6

I have a page called entry-main and it includes this template:

<template>
    <entry-search></entry-search>
    <entry-details></entry-details>
</template>

Inside <entry-search> there is another custom element. It looks like this:

<template>
    <div class="row">
        <div class="col-lg-12">
            <div class="input-group">
                <span id="entry_search" class="input-group-addon">
                    <span class="fa fa-search"></span>
                </span>
                <!-- important part -->
                    <typeahead id="choose" placeholder="Holding the place"></typeahead>
                <!-- end of important part -->
            </div>
        </div>
    </div>
</template>

Inside the typeahead viewmodel I'm getting the value of my typeahead like this:

$(this.id).on('typeahead:selected', function (e, item) {
       this.selected = item;
});

My question now is, how can I get the this.selected from my typeahead-viewmodel inside my entry-details-viewmodel?
And just for clarity, it should some what like this:

<entry-main>
    <entry-search>
          <typeahead></typeahead>
    </entry-search>

    <entry-details>
          ${selected}
    </entry-details>
</entry-main>
Jordec
  • 1,516
  • 2
  • 22
  • 43

2 Answers2

6

You could do this:

Create a property "selected" in entry-main:

export class EntryMain {
    selected = '';
    //rest of the code
}

Create a bindable property in typeahead:

import { bindable } from 'aurelia-framework';

export class Typeahead {
    @bindable selected;
    //... rest of the code
}

Bind the typeahead's "selected" to the entry-main's "selected"

<entry-main>
    <entry-search>
        <typeahead selected.bind="selected"></typeahead>
    </entry-search>

    <entry-details>
        ${selected}
    </entry-details>
</entry-main>

Untested code, but I think it should work.

Fabio
  • 11,892
  • 1
  • 25
  • 41
  • 3
    That could actually work. I haven't tested it myself though, but I used the `EventAggregator` from Aurelia. That way I could easily pass data from any viewmodel to any other random viewmodel. – Jordec Dec 24 '15 at 10:04
  • hey, @JorisDecraecker, that is awesome! you should definitely post it as an answer. – Eliran Malka Mar 16 '16 at 22:13
  • Why does this not work for me? I'm not using custom elements—is that necessary? – Ross Brasseaux Jun 23 '18 at 00:07
  • Yes. This solution only works for custom-elements. If you don't want to use them, you could check the solution in the first comment. – Fabio Jun 25 '18 at 18:17
0

As @JorisDecraecker says it could be done with EventAggregator. Example code that can be modified to your needs:

app.html

<template>
   <button click.delegate = "publish()">PUBLISH</button><br/>
   <button click.delegate = "subscribe()">SUBSCRIBE</button><br/>
   <button click.delegate = "dispose()">DISPOSE</button>
</template>

app.js

import {inject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';

@inject(EventAggregator)
export class App {
   constructor(eventAggregator) {
      this.eventAggregator = eventAggregator;
   }

   publish() {
      var payload = 'This is some data...';
      this.eventAggregator.publish('myEventName', payload);
   }

   subscribe() {
      this.subscriber = this.eventAggregator.subscribe('myEventName', payload => {
         console.log(payload);
      });
   }

   dispose() {
      this.subscriber.dispose();
      console.log('Disposed!!!');
   }
}

Source:

https://www.tutorialspoint.com/aurelia/aurelia_event_aggregator.htm

Ogglas
  • 62,132
  • 37
  • 328
  • 418