Skip to content

Instantly share code, notes, and snippets.

@Jeiwan
Created January 17, 2015 15:26
Show Gist options
  • Select an option

  • Save Jeiwan/6776368f2f02fe474b18 to your computer and use it in GitHub Desktop.

Select an option

Save Jeiwan/6776368f2f02fe474b18 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
</head>
<body>
<script id="jsbin-javascript">
var Item = Backbone.Model.extend({
defaults: {
title: ''
}
});
var List = Backbone.Collection.extend({
model: Item
});
var ListView = Backbone.View.extend({
el: $('body'),
events: {
'click button#add': 'addItem'
},
initialize: function() {
this.collection = new List();
this.collection.bind('add', this.appendItem);
this.render();
},
render: function() {
$(this.el).append('<input id="item-input" type="text" /><button id="add">Add</button>');
$(this.el).append('<ul id="list"></ul>');
},
addItem: function() {
var $input = $('#item-input');
if ($input.val().length > 0) {
var item = new Item({title: $input.val()});
this.collection.add(item);
$input.val('');
} else {
alert('Item is empty!');
}
},
appendItem: function(item) {
$('#list').append('<li>' + item.get('title') + '</li>');
}
});
var listView = new ListView();
</script>
<script id="jsbin-source-javascript" type="text/javascript">var Item = Backbone.Model.extend({
defaults: {
title: ''
}
});
var List = Backbone.Collection.extend({
model: Item
});
var ListView = Backbone.View.extend({
el: $('body'),
events: {
'click button#add': 'addItem'
},
initialize: function() {
this.collection = new List();
this.collection.bind('add', this.appendItem);
this.render();
},
render: function() {
$(this.el).append('<input id="item-input" type="text" /><button id="add">Add</button>');
$(this.el).append('<ul id="list"></ul>');
},
addItem: function() {
var $input = $('#item-input');
if ($input.val().length > 0) {
var item = new Item({title: $input.val()});
this.collection.add(item);
$input.val('');
} else {
alert('Item is empty!');
}
},
appendItem: function(item) {
$('#list').append('<li>' + item.get('title') + '</li>');
}
});
var listView = new ListView();</script></body>
</html>
var Item = Backbone.Model.extend({
defaults: {
title: ''
}
});
var List = Backbone.Collection.extend({
model: Item
});
var ListView = Backbone.View.extend({
el: $('body'),
events: {
'click button#add': 'addItem'
},
initialize: function() {
this.collection = new List();
this.collection.bind('add', this.appendItem);
this.render();
},
render: function() {
$(this.el).append('<input id="item-input" type="text" /><button id="add">Add</button>');
$(this.el).append('<ul id="list"></ul>');
},
addItem: function() {
var $input = $('#item-input');
if ($input.val().length > 0) {
var item = new Item({title: $input.val()});
this.collection.add(item);
$input.val('');
} else {
alert('Item is empty!');
}
},
appendItem: function(item) {
$('#list').append('<li>' + item.get('title') + '</li>');
}
});
var listView = new ListView();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment