ngUpgraders / ng-forward

The default solution for those that want to write Angular 2.x style code in Angular 1.x
411 stars 36 forks source link

Make 'ctrl' the default 'controllerAs' value #6

Closed timkindberg closed 8 years ago

timkindberg commented 9 years ago

How would we like to configure ng-forward? There are various options I think that we may like to provide to devs. Some examples are:

timkindberg commented 9 years ago

One idea is to do like ng-decorate does. Create a 'defaults' export. You can then do this:

import defaults from 'defaults'
defaults.globalControllerAsName = 'vm'

This would probably only be feasible as long as configs were set before bootstrap was called.

MikeRyanDev commented 9 years ago

So my opinion on this (though I don't feel too strongly about it) is that there should be no global configuration. If you pull in a package that depends on ng-forward and you've configured ng-forward in a way that the package did not expect, you've broken the package.

Instead, if each decorator is a pure function (which they currently are) then wrapping them in your own decorator is incredibly easy. For instance, if you worked at the Acme company and you wanted all of your components prefixed with acme and you wanted controllerAs always set to vm you could do this:

import {Component} from 'ng-forward';
import {componentWriter, providerWriter} from 'ng-forward/writers';

export default const AcmeComponent = config => t => {
  Component(config)(t);
  let name = providerWriter.get('name', t);
  providerWriter.set('name', `acme${name[0].toUpperCase()}${name.slice(1)}`, t);
  componentWriter.set('controllerAs', 'vm', t);
}

While that's not at all intuitive right now, I'd rather provide sugar for wrapping our decorators rather than exposing global configuration. The plus side of this is that you can probably do the same thing with the actual Angular 2 decorators.

timkindberg commented 9 years ago

That all makes sense. I'm in agreement.

So perhaps I'd rather just put a vote in for us using 'vm' over a constructed name. Three reasons:

I'd entertain other names as well: self, ctrl, me

aciccarello commented 9 years ago

I agree with @timkindberg on controllerAs.

You also mentioned configuring the click event. If both of these configurations are a simple string or boolean value, setting them up shouldn't be too difficult if you want to override the default.

ability to use either (click) or on-click syntax in templates (since doing both is too much overhead)

Also: Could you explain (or post a reference to) what the overhead is? I'm all for keeping things lean but I'm curious.

timkindberg commented 8 years ago

I wanted to add another great reason to use 'vm' (or similar) as the default in addition to the reasons I've already given.

@MikeRyan52 what do you say? I feel pretty strongly that the auto-generated camel cased controllerAs name is just not a good idea. It doesn't polyfill any existing ng2 functionality either. So it's basically an opinion forced on the dev by us. I'd rather have 'vm'.

timkindberg commented 8 years ago

After lengthy and passionate discussions on gitter, we've decided to change the default controllerAs to be 'ctrl' for easier migration later. Slotted for Milestone 2.