Deka87 Deka87 - 25 days ago 8
jQuery Question

Use function to dynamically update another function's arguments object

I use a simple plugin to add some functionality to my template:

$('.foo').ellipsis({
lines: 3
});


What I want to indicate a number of lines to show with a function instead of a number:

$('.foo').ellipsis({
lines: function() { return 3 }
});


This doesn't work for some reason. I am pretty sure this is not the plugins fault, but my misunderstanding of how it works. Any ideas?

A codepen is at:

http://codepen.io/Deka87/pen/qqNzZb

Answer

For that code to work, the plugin would have to be built to accept a function as a value for the lines property. From the source, the only valid values for lines that I note are the string "auto" or a numeric value.

You would need to do something like this:

$('.foo').each(function() {
   $(this).ellipsis({ lines: $(this).data('truncate-lines') });
});

If you want to modify the plugin to support your code, adding the following as the first lines of code in init() would probably do the trick:

if(typeof base.opts.lines === 'function') {
   base.opts.lines = base.opts.lines.call(el);
}