Skip to content

Instantly share code, notes, and snippets.

@gavinengel
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavinengel/e1346703b005f494d455 to your computer and use it in GitHub Desktop.
Save gavinengel/e1346703b005f494d455 to your computer and use it in GitHub Desktop.
Eventscript (proposed concise, nested script for DOM event handling) vs Javascript
// 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' }
}
// various ways of handling a click in javascript...
$('#foo').click(function() ...)
$('#foo').on('click', function() ...)
myEl.addEventListener('click', function() { ... })
// eventscript
#foo:click { ... }
// 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
}
// 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