Skip to content

Idea to specify bindings with annotations / decorators on methods rather than in the host configuration #3267

Closed

Description

@gionkunz

Wouldn't it be nicer to have something like this:

@Component({
  selector: 'comp'
})
@View({
  template: ''
})
export default class Comp {
  @Property({expression: '[attr.test]'})
  set testProp(value: string) {}

  @Event({name: 'click', stopPropagation: false})
  onClick(event: MouseEvent) {}
}

Rather than this:

@Component({
  selector: 'comp',
  host: {
    '(click)': 'onClick($event)',
    '[attr.test]': 'testProp'
  }
})
@View({
  template: ''
})
export default class Comp {
  testProp: string;
  onClick(event: MouseEvent) {}
}

I know it's gonna be a bit different than working in a template but don't you think this is much more convenient?

Activity

  1. gionkunz commented on Jul 24, 2015

    @gionkunz
    ContributorAuthor

    Also it allows to specify options (and not rely on hard to remember and limited symbol characters)

  2. pkozlowski-opensource commented on Jul 24, 2015

    @pkozlowski-opensource
    Member

    Also it allows to specify options (and not rely on hard to remember and limited symbol characters)

    @gionkunz those are the exact same characters as the ones used in a template. So I don't thin we've got a problem of "limited" symbol characters - or said differently - if we've got the pb here we've got it in templates as well.

    I would rather keep the syntax that is consistent with we've got in templates (after all we are manipulating a DOM element here) rather than introduce a new syntax.

  3. gionkunz commented on Jul 24, 2015

    @gionkunz
    ContributorAuthor

    Yeah, but using string tokens to configure a component within JavaScript just feels wrong. The token approach is a consequence of being limited to text in HTML but in JavaScript I thinks it's wrong to keep this limitation just for the sake of consistency.

    One other benefit of having annotations / decorators on accessors and methods would be that they are contextual at the right place. Even if you look at just a very simple example like the material button https://github.com/angular/angular/blob/master/modules/angular2_material/src/components/button/button.ts#L12-L37 . It's already a bit confusing to associate host bindings to functions and properties.

    I can imagine tooling could also work better by using decorators to specify bindings within components.

  4. pkozlowski-opensource commented on Jul 24, 2015

    @pkozlowski-opensource
    Member

    using string tokens to configure a component within JavaScript just feels wrong

    Could you elaborate on "just feels wrong"? For me it "feels right" since we are specifying part of host element template really. There was one proposal for this syntax to look like: host: '(click)=onClick($event) [tabIndex]=tabIndex' to be even closer to what you write in a template.

    Anyway, we need stronger arguments than feelings here :-)

  5. pkozlowski-opensource commented on Jul 24, 2015

    @pkozlowski-opensource
    Member

    My mental model here is this: everything on the host should be part of the host element template. It is like extending host element template from a directive.

  6. gionkunz commented on Jul 24, 2015

    @gionkunz
    ContributorAuthor

    Is it? But the scope is different right? If I'm adding a event binding on a component element in HTML, i'll never work in the context of the component itself but in the context where I included the directive. Am I completely wrong here?

    With "feels wrong" I just think that writing any kind of template fragment inside of JavaScript is wrong as if we look at the problem like this:

    A (view template with property binding) >>creates>> B (Directive Binding) >>on>> C (Directive)
    

    Then, writing some fragments back to the View would be like this:

    C >>creates>> A >>creates>> B >>on>> C
    

    Which could be simplified by:

    C >>creates>> B >>on>> C
    

    I'm sorry for my invented notation but I don't have access to any drawing tools right now :)) I hope this makes at least a little bit of sense.

  7. gionkunz commented on Jul 24, 2015

    @gionkunz
    ContributorAuthor

    It's the same as if someone would use a configuration file (template) or the API of the same product (Annotations)

  8. mhevery commented on Jul 27, 2015

    @mhevery
    Contributor

    Similar to: #1047

  9. self-assigned this
    on Sep 3, 2015
  10. added this to the alpha-37 milestone on Sep 3, 2015
  11. vsavkin commented on Sep 10, 2015

    @vsavkin
    Contributor

    Fixed 896add7

  12. gionkunz commented on Sep 11, 2015

    @gionkunz
    ContributorAuthor

    Awesome work @vsavkin !

  13. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

effort2: daysfeatureLabel used to distinguish feature request from other issues

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions