简体   繁体   中英

what does the `run() {}` do in javascript/Aurelia?

I see in Aurelia site, one of the article uses run() {} . What does this method in general do? it is a lifecycle hook or it is a new Javascript 2016 method?

http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/7

 import {Redirect} from 'aurelia-router'; export class App { configureRouter(config) { config.title = 'Aurelia'; config.addPipelineStep('authorize', AuthorizeStep); config.map([ { route: ['welcome'], name: 'welcome', moduleId: 'welcome', nav: true, title:'Welcome' }, { route: 'flickr', name: 'flickr', moduleId: 'flickr', nav: true, auth: true }, { route: 'child-router', name: 'childRouter', moduleId: 'child-router', nav: true, title:'Child Router' }, { route: '', redirect: 'welcome' } ]); } } class AuthorizeStep { run(navigationInstruction, next) { if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) { var isLoggedIn = /* insert magic here */false; if (!isLoggedIn) { return next.cancel(new Redirect('login')); } } return next(); } } 

You can add multiple pipeline steps to your router config. Each of the pipelines must implement PipelineStep interface:

interface PipelineStep {
  /**
   * Execute the pipeline step. The step should invoke next(), next.complete(),
   * next.cancel(), or next.reject() to allow the pipeline to continue.
   *
   * @param instruction The navigation instruction.
   * @param next The next step in the pipeline.
   */
  run(instruction: NavigationInstruction, next: Next): void;
}

( source code )

As you can see there must be run method. At some point later run methods from all the steps will be executed.

So the answer to your question: no, it's not something ES2015 introduces, but rather a convention pipeline steps must follow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM