Last active
August 29, 2015 13:58
-
-
Save JoshCheek/10344466 to your computer and use it in GitHub Desktop.
JS function that returns a function in order to bind an attribute.
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
function markRowHidden(dataAttribute) { | |
return function() { | |
var tr = $(this).closest('tr'); | |
tr.data(dataAttribute, true); | |
tr.hide(); | |
} | |
} | |
$("tr form.type1").on("submit", markRowHidden("attribute1")); | |
$("tr form.type2").on("submit", markRowHidden("attribute2")); |
Re: Dom data vs js state:
If you're mutating, it's state. I like to isolate my application state in tiny js data structures The rest of the app runs as a function of those microscopic objects and the collections fetched from the server.
This article is about react, but doesn't require the use of react: http://facebook.github.io/react/blog/2013/11/05/thinking-in-react.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: One change, I made the selectors different, so that you couldn't combine them like @theotherzach did (the two change events now target different form elements).
@theotherzach Thanks. I like how you moved the jquery wrapper up top. I was already annoyed by how each function was having to do that. Also like the array idea, I had a similar but shittier idea in mind. Regarding "consider moving this to a JS object if possible", what's the advantage of that over leaving it on the DOM element, essentially treating that element as a data structure?
@daytonn I translated my problem a bit wrong, the selector for the two is actually different. There are rows in a table, each row can be hidden for as many as two reasons (it has been marked inactive, or it has been permanently hidden). For each of the two reasons, there is a form in the row that will send a PATCH request to the server to hide that row. Now I need to remove it from the form. But there is also a button to show inactive rows, and another button to show permanently hidden rows, so I need to keep track of why that row is hidden. Thus the same javascript code (hide the element, set its class or data attribute so we can remember why), but with different values.