micjamking / succinct

A tiny jQuery plugin for truncating multiple lines of text
http://mikeking.io/succinct/
MIT License
477 stars 67 forks source link

Knockout JS binding version? #8

Open grofit opened 10 years ago

grofit commented 10 years ago

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>

micjamking commented 10 years ago

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:

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 :wink:), 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!

query-wow commented 8 years ago

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

    }

}