Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for module-global view-strategies. #13

Closed
Alxandr opened this issue Jan 31, 2015 · 1 comment
Closed

Allow for module-global view-strategies. #13

Alxandr opened this issue Jan 31, 2015 · 1 comment
Assignees

Comments

@Alxandr
Copy link

Alxandr commented Jan 31, 2015

Currently I use the following to set the view strategy (view factory factory) of my view model:

import {Router} from 'aurelia-router';

import view from './app.html!';

export class App {
  static inject() { return [Router]; }
  constructor(router) {
    this.router = router;
    this.router.configure(config => {
      config.title = 'Application Title';
      config.map([
        { route: 'blog',      moduleId: './blog/routes/index', nav: true, title: 'Blog' },
        { route: '',          moduleId: './redirect', redirect: '/blog' }
      ]);
    });
  }

  getViewStrategy() {
    return view;
  }
}

I would really like it though, if I could do something like this instead

import {Router} from 'aurelia-router';

export {view} from './app.html!';

export class App {
  static inject() { return [Router]; }
  constructor(router) {
    this.router = router;
    this.router.configure(config => {
      config.title = 'Application Title';
      config.map([
        { route: 'blog',      moduleId: './blog/routes/index', nav: true, title: 'Blog' },
        { route: '',          moduleId: './redirect', redirect: '/blog' }
      ]);
    });
  }
}

This basically creates a property on the module, named view, which acts as a module-global view factory factory.

@Craga89
Copy link

Craga89 commented Jan 31, 2015

This could currently be done by overriding the ViewStrategy classes getDefault method:

import {ViewStrategy} from 'aurelia-templating';
import {Metadata, Origin} from 'aurelia-metadata';

ViewStrategy.getDefault = function(target) {
     var strategy, annotation;

    if(typeof target !== 'function'){
      target = target.constructor;
    }

    annotation = Origin.get(target);
    strategy = Metadata.on(target).first(ViewStrategy);

    if(!strategy){
      if(!annotation){
        throw new Error('Cannot determinte default view strategy for object.', target);
      }
      // Override this part
      strategy = new MyCustomViewStrategy(annotation.moduleId);
    }
    else if(annotation){
      strategy.moduleId = annotation.moduleId;
    }

    return strategy;
}

Though it would be nice if this method hooked into some kind of global config option like defaultViewStrategy that we could override, rather than re-declaring the whole method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants