Knockout JS binding version?
grofit opened this issue · 2 comments
Hey nice plugin, I was looking over the source code and there does not seem to be any inherent dependency on jquery, so I was wondering if it would be possible to just remove the jquery bits and use normal DOM objects for the element interactions and make a KnockoutJS plugin version?
If you don't want to but dont mind me copying and editing your code I would be happy to make it myself :)
So you could do something like:
<p data-bind="text: Description, succinct: 255">blah blah blah</p>
Possibly. I have thought about creating different versions of Succinct for other libraries, particularly AngularJS, and even a Pure JS version. As a bit of background, I decided to use jQuery for a few reasons:
- The plugin format for jQuery is pretty elegant and simple.
-
= IE8 selector simplicity
- IE8 doesn't support the native String
.trim
method, so I use jQuery's$.trim()
to polyfill support (see here). I know it can be done without jQuery doing the following:
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
...but for the sake of keeping the plugin code succinct (see what I did there 😉), I opted to just use jQuery's method. For the pure JS version, this will be the path I'll go down.
I have very little experience with KnockoutJS, but I'm interested in learning more so this may be a good opportunity for me to learn. I'd definitely like to address issue #6 first, but I will look in to creating other versions as well. Your more than welcome to fork it and take a stab at it as well. It would be a great addition option!
If people still use this, the knockout version would be something like this
ko.bindingHandlers.truncate = {
init: function (element, valueAccessor, allBindings) {
$(element).text(valueAccessor());
$(element).succinct({
size: allBindings.get('size') || 100
});
}
}