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

Button down and up events, with and model example

Details
Written by: Stanko Milosev
Category: Knockout
Published: 21 January 2015
Last Updated: 21 January 2015
Hits: 4427

In this article I want to show example of with binding, buttondown and buttonup (useful for touchscreens, but in that case you will use touchstart, touchend).

HTML:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/myButtons.js"></script>
		<script type="text/javascript" src="/index.js"></script>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
	</head>
	
	<body onload="doTheApply()">
		<span data-bind="template: { name: 'myTemplate'}"></span>
		
		<script type="text/html" id="myTemplate">
			<div data-bind="with: myButtons">
				<div data-bind="event: {mousedown: down}">I will be executed on mouse down</div>
				<div data-bind="event: {mouseup: up}">I will be executed on mouse up</div>
			</div>
		</script>		

	</body>

</html>

Here notice with: myButtons.

myButtons.js:

window.myWithButtonsModel = window.myWithButtonsModel || {};

(function(ns) {
	ns.myWithButtonsModel = function() {
		var myButtons = new Button();
	
		function Button() {
		
			return {
				down: function() {
					console.log("Hi, I am called on button down event.");
				},
				up: function() {
					console.log("Hi, I am called on button up event.");
				}
			}
		}
		
		return {
			myButtons: myButtons,
		}		
		
	}

}(window));

Here notice how I introduced method myButtons

index.js:

function doTheApply() {
	model = new window.myWithButtonsModel();
	ko.applyBindings(model);
}

And here notice how applied model, this is basically same as from knockout example.

Example download from here.

Custom binding

Details
Written by: Stanko Milosev
Category: Knockout
Published: 21 October 2014
Last Updated: 21 October 2014
Hits: 4900

One simple example of custom binding. 

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>
		<div data-bind="myBinding: myObservable">Click me</div>
	</body>

</html>

JS:

ko.bindingHandlers.myBinding = {
	init: function (element, valueAccessor) {
		element.addEventListener("click", iWasClicked, false);
		function iWasClicked() {
			alert("I was clicked");
		}
	}
}

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

Init part will be always executed from ko.bindingHandlers.myBinding, and inside that method I implemented function iWasClicked.

Binding looks like this: data-bind="myBinding: myObservable" where myObservable actually I don't need.

Template name binding

Details
Written by: Stanko Milosev
Category: Knockout
Published: 26 September 2014
Last Updated: 26 September 2014
Hits: 4637

Idea is to change template on button click.

So, here is small example.

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="DoApply()">
		Persons:
		<script type="text/html" id="father-template">
			<button data-bind="text: name, click: myClick"></button>
		</script>
		
		<script type="text/html" id="childrens-template">
			<span data-bind="text: name, click: myClick"></span>
		</script>		

		<span data-bind="template: {name: getTemplate, foreach: myNames}"></span>
		
	</body>

</html>

JS:

function MyViewModel() {
	var self=this;
	self.myNames =  [
		{name: 'Stanko', template: ko.observable('father-template')},
		{name: 'Milosev', template: ko.observable('childrens-template')}
	]
	
	self.name = "test";
	self.fatherName = ko.observable("father-template");
	self.childrenName = ko.observable("childrens-template");
	self.template = ko.observable("childrens-template");
	
	myClick=function() {
		this.template() == "childrens-template" ? this.template("father-template") : this.template("childrens-template")
		//this.template("childrens-template");
	};
	
	self.getTemplate = function(data) {
		return data.template();
	}
}

function DoApply() {
	ko.applyBindings(new MyViewModel());
}

Example download from here.

forEach template small example

Details
Written by: Stanko Milosev
Category: Knockout
Published: 25 September 2014
Last Updated: 25 September 2014
Hits: 4479

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="DoApply()">
		Persons:
		<span data-bind="template: {name: 'persons-template', foreach: people}"></span>
		<script type="text/html" id="persons-template" >
			<h3 data-bind="text: name"></h3>
		</script>		
	</body>

</html>

JS:

function MyViewModel() {
	this.people = [
		{name: 'Stanko'},
		{name: 'Milosev'}
	]
}

function DoApply() {
	ko.applyBindings(new MyViewModel());
}

Download from here.

  1. The "template" binding
  2. The "click" binding
  3. CSS binding and observable
  4. Options binding

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 69 of 164

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