Navigation Menu

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

Specify element attributes directly in the DOM element constructor #3640

Closed
sethladd opened this issue Jun 14, 2012 · 12 comments
Closed

Specify element attributes directly in the DOM element constructor #3640

sethladd opened this issue Jun 14, 2012 · 12 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant library-html P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@sethladd
Copy link
Contributor

I'd really love to be able to do this:

var div = new DivElement.withAttrs({'id': 'foo', 'disabled':true, 'class':'bar'});

Ideally, this would be incredible:

var div = new DivElement(id: 'foo', disabled: true, class: 'bar');

If we're OK with a bunch of optional named parameters in the constructor. Which I am.

@vsmenon
Copy link
Member

vsmenon commented Jun 14, 2012

I feel your pain. :-)

I've got a CL to test out the latter form:

https://chromiumcodereview.appspot.com/10534131/

@sethladd
Copy link
Contributor Author

Whoa, did we just complete each other's sentences? :)

@rakudrama
Copy link
Member

Our plan of record here is to use method cascades.
The advantage of method cascades is that this kind of 'configuration' comes by default, and unlike passing a map, it is nicely typed with editor support.

If there is a problem with relying on method cascades, it needs to be fixed.

The list of attributes to add as optional parameters is large. Each additional optional parameter has a code size and performance tax since each optional parameter must be tested. A general map would require metadata for each element type to allow the attributes to be validated, otherwise methods and prototype chains could be replaced.

We can do the above, I'm just pointing out where we end up - a lot of code that is not really used.

With method cascades,

var div = new DivElement(id: 'foo', disabled: true, classes: ['bar']);

Becomes:

var div = new DivElement()..id = 'foo' ..disabled = true ..classes = ['bar'];

(You can do this today in dart2js, and it could be made a priority for the VM.)

FWIW, the original proposal for method cascades looked a little different, it would have looked more like this:

var div = new DivElement().{id = 'foo'; disabled = true; classes = ['bar']};

If you think method cascades do not deliver in terms of expressiveness, please open an issue against the language.


cc @larsbak.

@DartBot
Copy link

DartBot commented Jun 18, 2012

This comment was originally written by @seaneagan


Elements happen to be very mutable, but for final fields, method cascades as currently specified don't help with construction in this way. It would be nice if method cascades involving final fields were allowed to be attached to constructor calls. For example, with Date, the isUtc parameter is merely tacked on to the end of each constructor, which might make adding new constructor parameters akward in the future. If method cascades could be used then this would not be an issue:

new Date.now()..isUtc = true;

Not sure if any special syntax would be required. I think with the original cascade syntax, it might look like:

new Date.now(){isUtc = true};

Also, the parameters would probably need to be made available to factory constructors somehow.

@DartBot
Copy link

DartBot commented Jun 18, 2012

This comment was originally written by @seaneagan


I guess type authors though may wish to restrict setting final parameters at all, so maybe a better solution would allowing defining parameters which can only be specified by name, not by position. I think Gilad mentioned on the list that he was working on a proposal to orthogonalize named and optional parameters.

@iposva-google
Copy link
Contributor

Removed Area-DOM label.
Added Area-HTML label.

@vsmenon
Copy link
Member

vsmenon commented Aug 17, 2012

Added this to the Later milestone.

@efortuna
Copy link
Contributor

efortuna commented Oct 3, 2013

Added html-api label.

@blois
Copy link
Contributor

blois commented Oct 3, 2013

Removed html-api label.

@kevmoo
Copy link
Member

kevmoo commented Apr 7, 2014

Removed Area-HTML label.
Added Area-Library, Library-Html labels.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@sethladd sethladd added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-html labels Aug 4, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant library-html P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

10 participants