aurelia / framework

The Aurelia 1 framework entry point, bringing together all the required sub-modules of Aurelia.
MIT License
11.75k stars 623 forks source link

ValueConverter naming convention is confusing when using from a plugin #159

Closed paulvanbladel closed 9 years ago

paulvanbladel commented 9 years ago

I have a plugin with exposes a value converter in the following manner I'm assuming here that I'm doing it in the way it should be done, I'm using the aurelia.globalizeResources.

export function configure(aurelia, configCallback){
    aurelia.globalizeResources('./authFilter');
    var baseConfig = aurelia.container.get(BaseConfig);
    if(configCallback !== undefined && typeof(configCallback) === 'function')
    {
        configCallback(baseConfig);
    }
};

Now, the file authFilter contains the actual valueConverter class named AuthFilterValueConverter, so starting with a capital letter and ending with ValueConverter.

The way I need to use it as global resource inside a piece of html is:

<li repeat.for="row of router.navigation | authFilter: isAuthenticated" class="${row.isActive ? 'active' : ''}">
          <a data-toggle="collapse" data-target="#bs-example-navbar-collapse-1.in" href.bind="row.href">${row.title}</a>
        </li>

So,, as authFilter starting with a lower case letter.

Is there not a way to register the the value converter in a more transparant way with aurelia.container.registerSingleton ?

I tried things, but didn't work. Note, that the technique above aurelia.globalizeResources works. I think that the convention that ValueConverter is stripped off is ok, but it feels like the name authFilter refers in the first place to the name of file rather than the class AuthFilterValueConverter.

EisenbergEffect commented 9 years ago

I'm not clear on what the problem/question is.

paulvanbladel commented 9 years ago

Sorry, I'll summarize the questions?

  1. is authFilter referring here to the file name 'authFilter' rather than the class AuthFilterValueConverter (where a convention is applied to strip off ValueConverter) . In case it refers to the fileName, how could I have multiple value converters in one file ?
  2. Is the above way the correct way to register a valueconverter inside a plugin, or could it be done via the aurelia container as well? (.e.g. via aurelia.container.registerSingleton)
EisenbergEffect commented 9 years ago

Gotcha.

The value converter name is not derived from the file, but from the export name of the class, with "ValueConverter" stripped and then camel cased. You can always explicitly name the value converter by applying a decorator like this @valueConverter('foo') You can have multiple value converters exported from a single module.

Using globalizeResources is the proper way to make plugin resources globally available to the view system.

paulvanbladel commented 9 years ago

Thanks Rob. Appreciated. I'm just trying to apply the most aesthetic way of working with plugins. Ok, so a value converter is seen as a real resource , like an html template.

What confuses me, is what is the actual use case for adding something in the aurelia container via aurelia.container.registerSingleton or the other methods. (i understand in general the use case for containers, like MEF and others)

In case i want to expose a simple class (e.g. a service) from the plugin to the consuming app I could simply write in the index.js file of the plugin .

export {MyClass} from './myClass'; 

and the consuming app could use it as follows:s

import {MyClass} from 'my plugin package name';

Could you please indicate how in this scenario registering MyClass in the aurelia container would be useful?

Thanks in advance.

EisenbergEffect commented 9 years ago

You register your class in the container if you want consumers to able to inject instances of it into their classes...or if your class has dependencies that you want it to receive from the container. It's the same as any DI use case.

(Incidentally, your value converter is actually instantiated by the DI container. It just needs to be registered as a resource because that tells the compiler how to map various identifiers it finds while compiling views to actual classes that get instanced by the DI.)