milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

The "template" binding

Details
Written by: Stanko Milosev
Category: Knockout
Published: 19 September 2014
Last Updated: 19 January 2015
Hits: 4782

Title is taken from here. I just wanted to write my small example of template binding for future reference.

HTML:

<!DOCTYPE html>
<html>
	
	<head>
		<script type="text/javascript" src="/index.js"></script>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
	</head>
	
	<body onload="doTheApply()">
		Here is example from knockout:
		<span data-bind="template: { name: 'person-template', data: buyer}"></span>
		
		<script type="text/html" id="person-template">
			<h3 data-bind="text: name"></h3>
			<p>Credits: <span data-bind="text: credits"></span></p>
		</script>
		
	</body>
	
</html>

JS:

myViewModel = {
	buyer: {name: "Franklin1", credits: 450}
}

function doTheApply() {
	ko.applyBindings(myViewModel);
}

Example download from here.

---

Another example:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
	</head>

	<body>
		My template:
		<script type="text/html" id="myTemplate">
			<input data-bind="attr: {type: inputType}" placeholder="I am going to be changed">
		</script>
		
		<span data-bind="template: {name: 'myTemplate', data: {inputType: 'search'}}">
		
	</body>
	
	<script type="text/javascript">
		ko.applyBindings();
	</script>

</html>

Here notice line:

<span data-bind="template: {name: 'myTemplate', data: {inputType: 'search'}}">

As you can see, $data I bind directly in the template definition.

---

One example with radio buttons:

HTML:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
		<script type="text/javascript" src="/index.js"></script>
	</head>
	
	<body>
		
		<span data-bind="template: { name: 'myRadioButtons', foreach: myRadioButton }"></span>
		
		<script type="html/text" id="myRadioButtons">
			Test: <input type="radio" data-bind="checked: myValue"><p/>
		</script>
		
	</body>

</html>

JS:

var myRadioButtonViewModel =  {
	myRadioButton:  [
		{myValue: 1}, 
		{myValue: 2}
	]
}

window.onload = function () {
	ko.applyBindings(myRadioButtonViewModel);
}

The "click" binding

Details
Written by: Stanko Milosev
Category: Knockout
Published: 12 September 2014
Last Updated: 15 September 2014
Hits: 4820

Title taken from here. For my example I did it with input.

Html:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
		<script type="text/javascript" src="/index.js"></script>
	</head>

	<body onload="ko.applyBindings(viewModel)">
		<input type="checkbox" placeholder="I am a test" data-bind="click: inputClick"></input>
	</body> 

</html>

JS:

var viewModel = {
	inputClick: function() {
		alert("just clicked input");
                return true;
	}
}

Here note return true. Without that state will not change (i.e. if for input you write type="checkbox")

Download from here.

CSS binding and observable

Details
Written by: Stanko Milosev
Category: Knockout
Published: 09 September 2014
Last Updated: 16 September 2014
Hits: 4215

With observables we can automatically update our binding, and CSS binding is to apply CSS or not. Check this code:

HTML:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
		<script type="text/javascript" src="/index.js"></script>
		<link rel="stylesheet" type="text/css" href="/index.css">
	</head>

	<body onload="ko.clearBinding; ko.applyBindings(someModel)">
		<button onclick="someModel.makeMeBold(!someModel.makeMeBold()); someModel.someText('frsfd')">CSS test</button>
		I am another test <strong data-bind="text: someText"></strong>
		<p data-bind="css: {knockoutTest: makeMeBold()}">Now I want some css to be binded.</p>
	</body>

</html>

JS:

someModel = {
	someText: ko.observable("I am a test!"),
	makeMeBold: ko.observable(true)
}

CSS:

p.knockoutTest {
	font-weight: bold;
}

p.makeMeItalic {
	font-style: italic;
}

Note how I called:

someModel.makeMeBold()

and set "makeMeBold":

someModel.makeMeBold(!someModel.makeMeBold())

We are just setting css visible true or false.

Example download from here.

Options binding

Details
Written by: Stanko Milosev
Category: Knockout
Published: 01 October 2013
Last Updated: 01 October 2013
Hits: 1279

Just a short note to myself, what we need for options binding, so that it can work properly. 

<select autowidth="true" data-bind="
	options: $root.carTypes
	,optionsValue: 'carValue'
	,value: myTest
"></select>

Where myTest is like:

function CarsView(initialValues) {
	var self = this;	
	self.myTest = ko.observable(initialValues);
}

If we need model to pass, then model should look like:

carsModel = 
	[
		{ 
			carTypes: ko.observable('Ferrari'),
			myTest: ko.observable('Ferrari')
		}
	];
  1. Options binding
  2. Attributes
  3. applyBindings
  4. Closing tags

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 70 of 164

  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74