Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Functions In Aurelia

I'm trying to figure out how to store a "global" like function in Aurelia. I've followed this tutorial "http://blog.durandal.io/2015/04/24/aurelia-custom-elements-and-content-selectors/" to open a modal with a dynamic view modal, but I can't figure out where I should actually put this function so I can re-use it all my view routes.

I've created this function in my default view:

//open modal
setModal(modal) {
    this.contentModal = modal;
    $('.modal').modal();
}

with this markup inside that view template:

<a click.delegate="setModal('users')">Test</a> <a click.delegate="setModal('child-router')">Test 2</a>
<modal>
    <modal-header title.bind="'View Person'"></modal-header>
    <modal-body content.bind="contentModal"></modal-body>
    <modal-footer buttons.bind="['Cancel']"></modal-footer>
</modal>

And I can call it via click.delegate="setModal('users') inside that view template, but I can't figure out how to actually make this available outside of this view template.

Sorry I'm very new to this framework!

like image 415
Ben Kilah Avatar asked Aug 18 '15 08:08

Ben Kilah


People also ask

How do you define a global function in JavaScript?

Open JS console, write var x = function() {console. log('x')} and then try to call window. x() . In browsers, window is global scope, therefore the function is global.

How does the this keyword work provide some code examples?

In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword. Identifying the object in the current execution context when we invoke a method.


2 Answers

So it sounds like you have a default view + view-model, lets call them app.html and app.js.

In app.html you have the modal markup:

<modal>
    <modal-header title.bind="'View Person'"></modal-header>
    <modal-body content.bind="contentModal"></modal-body>
    <modal-footer buttons.bind="['Cancel']"></modal-footer>
</modal>

And in app.js you have the function to display a modal:

//open modal
setModal(modal) {
    this.contentModal = modal;
    $('.modal').modal();
}

And your question is "how do I share the setModal function with other view-models?"

You could register the setModal function in the container. Then you will be able to inject it into other view-models that have a dependency on that function:

app.js

import {inject, Container} from 'aurelia-framework'; // or 'aurelia-dependency-injection'

@inject(Container)
export class App {
  constructor(container) {
    // register the setModal function in the container
    // under the key "setModal".
    container.registerInstance('setModal', this.setModal.bind(this));
  }

  //open modal
  setModal(modal) {
    this.contentModal = modal;
    $('.modal').modal();
  }
}

some-other-view-model.js

import {inject} from 'aurelia-framework'; // or 'aurelia-dependency-injection'

@inject('setModal') // inject the setModal function into this view-model
export class SomeOtherViewModel {
  constructor(setModal) {
    // create a setModal property for binding purposes
    this.setModal = setModal;
  }
}

It also might be worth taking a look at the aurelia-dialog plugin. You might also wrap this up in a custom attribute so that you don't have to import the setModal function into your view-models.

like image 166
Jeremy Danyow Avatar answered Oct 15 '22 10:10

Jeremy Danyow


I would recommend using the globalResources function in your configuration.

An example would be

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .globalResources('scripts/global.js');

  aurelia.start().then( () => aurelia.setRoot('main.js'));
}
like image 43
ink Avatar answered Oct 15 '22 11:10

ink