<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form class="todo-form" data-todo-form>
<input type="text" data-text />
<input type="submit" value="Add" data-submit />
</form>
<div class="todo-list" data-todo-list>
<script type="text/html" data-todo-list-item-template>
<div class="item">
<input type="checkbox" data-done id="label-{{label_id}}" />
<label for="label-{{label_id}}" data-text>{{text}}</span>
</div>
</script>
</div>
</body>
</html>
$(function() {
// Load decorator
$.fn.startLoad = function(url, params, callback)
{
var self = $(this);
$.getJSON(url, params, function(data) {
callback(data);
self.trigger('finishLoad');
});
return self;
};
// Render template
$.fn.template = function(data)
{
var text = $(this).html();
text = text.replace(new RegExp('{{([a-zA-Z_\-]+)}}', 'ig'), function(str, key) {
return data[key] !== undefined ? data[key]: '';
});
return $($.parseHTML(text, document));
};
$.fn.todoItem = function(data)
{
var self = $(this);
data.time = new Date().getTime();
data.label_id = data.time + '-' + Math.ceil(Math.random() * 100);
self.data = data;
self.append($('[data-todo-list-item-template]').template(data));
var checkbox = self.find('[data-done]');
if (data.done !== undefined && data.done === true) {
self.find('[data-done]').attr('checked', true);
}
self.on('done', function(event, state) {
self.data.done = state;
self.toggleClass('done', self.data.done);
checkbox.attr('checked', state);
if (self.data.done === true) {
self.slideUp();
} else {
self.slideDown();
}
return false;
});
checkbox.on('change', function() {
self.trigger('done', [checkbox.is(':checked')]);
});
return self;
};
$.fn.todoList = function()
{
var self = $(this);
self.on('add', function(event, data) {
var item = $('<div />').todoItem(data);
self.append(item);
item.on('done', function() {
// alert('Task is: ' + (item.data.done ? 'DONED': 'WAIT'));
});
return false;
});
return self;
};
$.fn.todoForm = function()
{
var self = $(this);
self.on('submit', function(event) {
self.trigger('todoAdd', [{
text: self.find('[data-text]').val(),
done: false
}]);
return false;
});
return self;
};
var todoList = $('[data-todo-list]').todoList();
var todoForm = $('[data-todo-form]').todoForm();
$(document).on('todoAdd', function(event, data) {
todoList.trigger('add', [data]);
});
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |