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
  3. JavaScript

CSS binding and observable

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

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: 1277

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')
		}
	];

Options binding

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

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')
		}
	];

Not finished example you can see here.

Attributes

Details
Written by: Stanko Milosev
Category: Knockout
Published: 01 October 2013
Last Updated: 30 November -0001
Hits: 4270

If you want to bind attributes, then declare them under the quotes, something like:

 

attr: {id: 'availableSensorsId' + $index()}

Where $index is binding context

  1. applyBindings
  2. Closing tags
  3. self = this
  4. Autocomplete

Subcategories

Ajax

JavaScript

ExtJS


jQuery

Bing Maps AJAX Control, Version 7.0

Knockout

Jasmine

Moment

node.js

Page 18 of 24

  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22