Created
May 5, 2014 11:23
-
-
Save ninjabiscuit/11534194 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
.block { | |
min-height:50px; | |
background:blue; | |
margin-bottom:1em; | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<button id="createBlock">Create block</button> | |
<div id="container"></div> | |
</body> | |
</html> |
This file contains hidden or 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
var $container = $("#container"); | |
var $button = $("#createBlock"); | |
var input_events = "input change"; | |
var BlockArray = function() { | |
this.data = []; | |
}; | |
BlockArray.prototype = { | |
toJSON : function() { | |
return JSON.stringify(this.data); | |
}, | |
push : function() { | |
[].push.apply(this.data, arguments); | |
} | |
}; | |
var data = new BlockArray(); | |
$button.on("click", createBlock); | |
$container.on(input_events, function(){ | |
console.log(data.toJSON()); | |
}); | |
function createBlock() { | |
var block = new Block({"content":"contentediable","input":"input","textarea":"textarea", "animals":2}); | |
$container.append(block.$el); | |
data.push(block.data); | |
} | |
function Block(){ | |
this.initialize.apply(this, arguments); | |
} | |
Block.prototype = { | |
initialize : function(data) { | |
this.setElement(); | |
this._setData(data); | |
this.listenForUpdates(); | |
return this; | |
}, | |
onInput : function(e) { | |
this.data[e.currentTarget.dataset.stBlockAttr] = | |
e.currentTarget.value || e.currentTarget.innerHTML; | |
}, | |
listenForUpdates : function() { | |
this.$el.on(input_events, "[data-st-block-attr]", $.proxy(this.onInput, this)); | |
}, | |
_setAttribute : function(el, value) { | |
if (el.contentEditable === 'true' || el.nodeName === "TEXTAREA") { | |
el.innerHTML = value; | |
return; | |
} | |
el.value = value; | |
}, | |
_setData: function(data) { | |
this.data = data || {}; | |
if (!data) { return; } | |
for(var attr in data) { | |
this._setAttribute(this.$el.find("[data-st-block-attr="+attr+"]")[0], data[attr]); | |
} | |
}, | |
setElement : function() { | |
this.$el = $("<div>", { | |
"html":"<div class='block' contenteditable=true data-st-block-attr='content' data-st-block-required=true></div><input data-st-block-attr='input'><textarea data-st-block-attr='textarea'></textarea><select data-st-block-attr='animals'><option value='0'>Chicken</option><option value='1'>Crocodile</option><option value='2'>Monkey</option></select>" | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh yeah? Like this 👍