Created
March 27, 2014 11:45
-
-
Save adamrecsko/9805741 to your computer and use it in GitHub Desktop.
Knockout binding for cut text to the required length. It is a require js module. If it less then specified length then cut it and concatenate the trailing to the end of string.
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
define(['knockout'],function(ko){ | |
ko.bindingHandlers.cuttext = { | |
update: function (element, valueAccessor,allBindingsAccessor) { | |
var length = allBindingsAccessor().length || 4294967295; //SAFE MAX_INT | |
var trailing = allBindingsAccessor().trailing || ""; | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
if (length<value.length){ | |
value = value.substr(0,length)+trailing; | |
} | |
$(element).text(value); | |
} | |
}; | |
}; | |
/* | |
Examples: | |
with trailing: | |
<div data-bind="cuttext:yobservable , length: 300, trailing:'...'"></div> | |
without trailing: | |
<div data-bind="cuttext:yobservable , length: 300"></div> | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment