Created
June 18, 2015 16:10
-
-
Save norcal82/5a30b096d2b6c939b55d to your computer and use it in GitHub Desktop.
mini data binding
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
$('body') | |
.on 'focus', '[contenteditable]', -> | |
$this = $(this) | |
$this.data 'before', $this.html() | |
.on 'blur keyup paste input', '[contenteditable]', -> | |
$this = $(this) | |
if $this.data('before') isnt $this.html() | |
$this.data 'before', $this.html() | |
$this.trigger('change') | |
new_text = $this[0].innerText | |
# hacky. will bind form text and element text | |
$('.'+$this.data('bind-id')).val(new_text).text(new_text) |
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
// place data-input-id attr on element with a class that targets elements you want to bind to | |
// this will bind the contenteditable data to the input val | |
%h1{contenteditable: "", :"data-bind-id" => "name-bind"} My Name | |
%input{:class => "name-bind"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment