Created
November 16, 2015 14:35
-
-
Save rogersillito/cae046b2d9a1e50d24dc to your computer and use it in GitHub Desktop.
Knockout.js: Experminting with binding context variables
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> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div data-bind="foreach: teams"> | |
<h2 data-bind="text: name"></h2> | |
<span data-bind="foreach: players"> | |
<h3 data-bind="text: name"></h3> | |
<span data-bind="foreach: cars"> | |
<span data-bind="text: ($parentContext.$parentContext.$index()+1) + ', ' + ($parentContext.$index()+1) + ', ' + ($index()+1) +': '+ $data"></span> | |
<br data-bind="visible: $index() < ($parent.cars.length - 1)" /> | |
</span> | |
</span> | |
</div> | |
<script id="jsbin-javascript"> | |
var TestViewModel = function() { | |
this.teams = [ | |
{name:'Stoke City', players: [ | |
{name:'Jack Butland', cars:['Ford','Skoda']}, | |
{name:'Bojan Krkic', cars:['Seat','VW']} | |
]}, | |
{name:'Chelsea', players: [ | |
{name:'Asmir Begovic', cars:['Lada']}, | |
{name:'John Terry', cars:['Twatmobile','Peugeot']} | |
]}, | |
{name:'Liverpool', players: [ | |
{name:'Simon Mignolet', cars:['Seat','Skoda','Fiat','Land Rover']}, | |
{name:'Adam Lallana', cars:['Porsche']} | |
]} | |
]; | |
}; | |
ko.applyBindings(new TestViewModel()); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var TestViewModel = function() { | |
this.teams = [ | |
{name:'Stoke City', players: [ | |
{name:'Jack Butland', cars:['Ford','Skoda']}, | |
{name:'Bojan Krkic', cars:['Seat','VW']} | |
]}, | |
{name:'Chelsea', players: [ | |
{name:'Asmir Begovic', cars:['Lada']}, | |
{name:'John Terry', cars:['Gimpmobile','Peugeot']} | |
]}, | |
{name:'Liverpool', players: [ | |
{name:'Simon Mignolet', cars:['Seat','Skoda','Fiat','Land Rover']}, | |
{name:'Adam Lallana', cars:['Porsche']} | |
]} | |
]; | |
}; | |
ko.applyBindings(new TestViewModel());</script></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment