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

Support assignment of attribute litterals via interpolation #106

Open
MeirionHughes opened this issue Dec 13, 2016 · 3 comments
Open

Support assignment of attribute litterals via interpolation #106

MeirionHughes opened this issue Dec 13, 2016 · 3 comments

Comments

@MeirionHughes
Copy link

MeirionHughes commented Dec 13, 2016

a lot of third-party functionality is achieved via adding attributes to an element. Some of these attribute values can be quite large and/or can be best assigned via interpolation. It would be useful to support this for the sake of better integration with standards + third-party libraries.

As per @jdanyow's suggestion, introduciton of .attr binding could facilitate these use cases.

For example:

<input aria-valuemin.attr="${current - 10}" aria-valuemax.attr="${current + 10}"></input>

would result in the outcome (assuming current = 0):

<input aria-valuemin="-10" aria-valuemax.attr="10" ... 
@jdanyow
Copy link
Contributor

jdanyow commented Dec 14, 2016

The problem with the & attr binding behavior is the behavior has no way to tell what the target attribute's original name is. All the attribute names pass through the AttributeMap which typically converts the kebab-case HTML attribute name to a camelCase property name.

Example:

<div foo-bar.bind="baz & attr"></div>

"foo-bar" will be translated to "fooBar". el.setAttribute('fooBar', ...) is going to create an HTML attribute named "foobar". The developer wanted "foo-bar".

@grofit
Copy link

grofit commented Dec 14, 2016

I could really do with this feature, and just so there is a link to the use case which started it all:
aurelia/framework#669

@jdanyow
Copy link
Contributor

jdanyow commented May 20, 2017

Here's how to extend the binding syntax with a .attr command that will work like you expect:

import {SyntaxInterpreter} from 'aurelia-templating-binding';
import {BehaviorInstruction} from 'aurelia-templating';
import {BindingExpression, BindingBehavior} from 'aurelia-binding';

SyntaxInterpreter.prototype.attr = function(resources, element, info, existingInstruction, context) {
  const instruction = existingInstruction || BehaviorInstruction.attribute(info.attrName);

  const expression = new BindingBehavior(this.parser.parse(info.attrValue), 'attr', []);
  instruction.attributes[info.attrName] = new BindingExpression(
    this.observerLocator,
    info.attrName,
    expression,
    info.defaultBindingMode || this.determineDefaultBindingMode(element, info.attrName, context),
    resources.lookupFunctions
  );

  return instruction;
}

Usage: <foo bar-baz-beep.attr="something">
Result: <foo bar-baz-beep="hello world">

https://gist.run/?id=d736a4c59bed2032017fe286e74ec5ee

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