Last active
August 29, 2015 14:18
-
-
Save gavinengel/e1346703b005f494d455 to your computer and use it in GitHub Desktop.
Eventscript (proposed concise, nested script for DOM event handling) vs Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery | |
$('button').click(function(){ | |
$('h1, h2, p').addClass('blue') | |
$('div').removeClass('important') | |
$('h3').toggleClass('error') | |
$('#foo').attr('alt', 'Lorem Ipsum') | |
}); | |
// Eventscript | |
button:click { | |
h1, h2, p { [class] &: blue } | |
div { [class] ^: important } | |
h3 { [class] ~: error } | |
#foo { [alt]: 'Lorem Ipsum' } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// various ways of handling a click in javascript... | |
$('#foo').click(function() ...) | |
$('#foo').on('click', function() ...) | |
myEl.addEventListener('click', function() { ... }) | |
// eventscript | |
#foo:click { ... } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// javascript | |
$('#foo').on( 'click', function() { | |
if ($(this).attr('data-foo') > 100) { | |
var foo = $(this).attr('data-foo') | |
foo += 2 | |
$(this).attr('data-foo', foo) | |
var bar = $(this).attr('data-bar') | |
bar -= 5 | |
$(this).attr('data-bar', bar) | |
} | |
}) | |
// eventscript | |
#foo:click ([data-foo] > 100) { | |
[data-foo] +: 2 | |
[data-bar] -: 5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// javascript, where order of code can be strewn about, perhaps into separate files | |
// note how code is interwoven in arbitrary way: | |
$('#foo #bar .baz').addClass( 'important' ) | |
$('#bar .qux').addClass( 'highlight' ) | |
$('#foo').on('click', function() { | |
... | |
}).css('background-color', 'red') | |
$('#bar').on('click', function() { | |
... | |
}) | |
$('.baz').on('hover', function() { | |
... | |
}) | |
$('.qux').on('hover', function() { | |
... | |
}) | |
// eventscript, where code naturally organized, similar to how .scss organizes .css | |
// note code is nested in a concise way: | |
#foo { | |
background-color: red | |
&:click { | |
... | |
} | |
#bar { | |
.baz { | |
[class] &: important | |
&:hover { | |
... | |
} | |
} | |
.qux { | |
[class] &: highlight | |
&:hover { | |
... | |
} | |
} | |
&:click { | |
... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment