Last active
December 27, 2015 17:59
-
-
Save tomazy/7365898 to your computer and use it in GitHub Desktop.
backbone.js + knockout.js
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script> | |
<script type='text/javascript' src="http://underscorejs.org/underscore-min.js"></script> | |
<script type='text/javascript' src="http://backbonejs.org/backbone-min.js"></script> | |
<script type='text/javascript' src="http://knockoutjs.com/downloads/knockout-3.0.0.js"></script> | |
</head> | |
</body> | |
<template id="t"> | |
<select data-bind="options: tickets, | |
optionsCaption: 'Choose...', | |
optionsText: 'name', | |
value: chosenTicket"></select> | |
<button data-bind="enable: chosenTicket, | |
click: resetTicket">Clear</button> | |
<p data-bind="with: chosenTicket"> | |
You have chosen <b data-bind="text: name"></b> | |
($<span data-bind="text: price"></span>) | |
</p> | |
</template> | |
<script type='text/javascript' src="script.js"></script> | |
</body> |
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
var TestView = Backbone.View.extend({ | |
template: function(){ | |
return $('#t').html(); | |
}, | |
render: function(){ | |
this.$el.html(this.template()); | |
ko.applyBindings(this.model, this.el); | |
return this; | |
} | |
}); | |
function TicketsViewModel(model){ | |
this.tickets = [ | |
{name: 'Economy', price: 199.95}, | |
{name: 'Business', price: 449.95}, | |
{name: 'First Class', price: 1199.00} | |
]; | |
this.chosenTicket = ko.observable(); | |
this.resetTicket = function(){ | |
this.chosenTicket(null); | |
} | |
this.chosenTicket.subscribe(function(newValue){ | |
console.log('changing ticket to new value', newValue); | |
}); | |
} | |
var view1 = new TestView({ | |
model: new TicketsViewModel() | |
}); | |
var view2 = new TestView({ | |
model: new TicketsViewModel() | |
}); | |
$('body').append(view1.render().el) | |
$('body').append(view2.render().el) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment