Click here to Skip to main content
13,088,711 members (55,861 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.4K views
1 bookmarked
Posted 11 Oct 2013

Creating Custom (Values) Binding Handler for KnockOut.js

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
If you working in web development for a long time, you must have heard about knockout.js. It implements MVVM pattern in javascript. Siverlight guys

Editorial Note

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

If you working in web development for a long time, you must have heard about knockout.js. It implements MVVM pattern in javascript. Siverlight guys must be familiar with this word. This helps  to create rich, responsive display and editor user interfaces with a clean underlying data model. User Need not worry about binding the data model to your view. Any time , Your data changes your View automatically refreshes.

While working on a project, I was  asked to create a MultiSelect Box. As , I was using I thought of creating Custom binding handler so that I can use it time and again my code.

Below is the HTML :

1) Have a select element as below :

<select id=”customID” data-bind=”Values: ViewModelPropertyName”>

<option value=”1″>value1</option>

<option value=”2″>value2</option>

<option value=”3″>value3</option>

<option value=”4″>value4</option>

</select>

&nbsp;

<img id=”imgcustomID” src=”../add.png”/><br/>

<ul id=”ulcustomID”  class=”navlist”></ul>

2) Add below style in your css :
.navlist
{
margin-left: 0;
padding-left: 0;
list-style: none;
}
.navlist li
{
padding-left: 25px;
background-image: url(../images/minus.png);
background-repeat: no-repeat;
background-position: 0 .1em;
height:16px;
}

3) Add below Javascript Code  in your HTML Files:

<script type=”text/javascript”>

ko.bindingHandlers.Values = {
init: function (element, valueAccessor) {
var _this = ko.bindingHandlers.Values;
var id = jQuery(element).attr(‘id’);
jQuery(element).after(“<img src=’../../../images/add.png’ id=’img” + id + “‘ onClick=’javascript: ko.bindingHandlers.Values.setCommand(\”" + id.toString() + “\”);’ ></img><ul class=’navlist’ id=’ul” + id + “‘></ul>”);
var values = new Array();
var value = ko.utils.unwrapObservable(valueAccessor());
if (value) {
values[0] = value;
if (values[0] == “”)
values.splice(0, 1);
setTimeout(“ko.bindingHandlers.Values.addLI(‘” + id + “‘,’” + values + “‘,0);”, 300);
}
},
setCommand: function (id) {
var _this = ko.bindingHandlers.Values;
if (jQuery(‘#’ + id).attr(‘value’) !== “”) {
var select = document.getElementById(id);
if (select.selectedIndex >= 0)
_this.addLI(id, select.options[select.selectedIndex].value, 1);
}
},
addLI: function (id, values, mode) {
var _this = ko.bindingHandlers.Values;
var currentvalues = [];
var prop = id.replace(“slct”, “”);
var viewModelData = JSON.parse(ko.toJSON(viewModel));
if (viewModelData[prop]) {
if (viewModelData[prop].toString().search(“,”) !== -1) {
var currentvalues = eval(“viewModel.” + prop + “().split(‘,’)”);
}
else {
currentvalues[0] = eval(“viewModel.” + prop + “()”);
}
if (currentvalues[0] == “”)
currentvalues.splice(0, 1);
if (values !== “” && mode == 1)
currentvalues.pushIfNotExist(values);
eval(“viewModel.” + prop + “(‘” + currentvalues.join(‘,’) + “‘)”);
}
else {
eval(“viewModel.” + prop + “(” + values + “)”);
}
if (mode == 0) {
jQuery.each(currentvalues, function (key, value) {
var li = document.createElement(‘li’);
var select = document.getElementById(id);
li.innerHTML = _this.getText(id, value.trim());
li.customValue = value;
var ul = document.getElementById(‘ul’ + id);
li.onclick = function () {
var option = document.createElement(‘option’);
option.text = this.innerHTML;
option.value = this.customValue;
select.add(option);
ul.removeChild(li);
eval(“viewModel.” + prop + “(_this.getValues(‘” + id + “‘))”);
};
ul.appendChild(li);
_this.removeOption(id, value.trim());
});

}
else {
var li = document.createElement(‘li’);
var select = document.getElementById(id);
li.innerHTML = _this.getText(id, values);
li.customValue = values;
var ul = document.getElementById(‘ul’ + id);
li.onclick = function () {
var option = document.createElement(‘option’);
option.text = this.innerHTML;
option.value = this.customValue;
select.add(option);
ul.removeChild(li);
eval(“viewModel.” + prop + “(_this.getValues(‘” + id + “‘))”);
};
ul.appendChild(li);
_this.removeOption(id, values);
}
},
getValues: function (id) {
return jQuery(‘#ul’ + id + ‘ li’).map(function () {
return jQuery(this).attr(‘customValue’);
}).get().join(“, “);
},
getText: function (id, value) {
return jQuery(“#” + id + ” option[value='" + value + "']“).text();
},
removeOption: function (id, value) {
jQuery(“#” + id + ” option[value='" + value + "']“).remove();
}
};
});
}

var  viewModel = {};

viewModel .ViewModelPropertyName=’1,2′;

ko.applyBindings(viewModel);

</script>

4) Add two images add.png and minus.png into your images folder  and make sure they are been accessible from your css and html file.

Hopefully ths will be very useful for those who wants to use knockout.js in their projects. To use the above code please do not forget to add latest jQuery library and knockout.js

Happy Coding :)

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

765 members


You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid