ivirabyan / jquery-mentions

Adds mentioning support to your text fields.
http://ivirabyan.github.io/jquery-mentions/
MIT License
114 stars 49 forks source link

Added new function setValue #7

Closed pergardebrink closed 9 years ago

pergardebrink commented 9 years ago

This was I function that I needed in order to set the value after it had been initialized. Maybe it could be useful for others as well? I'm new to GitHub so I apologize if I'm doing this pull request wrong.. ;)

$('.mentions').mentionsInput('setValue', 'Hey @[alex](user:1)');

I also fixed a bug in the init function that checks if it's an INPUT, TEXTAREA element

ivirabyan commented 9 years ago

This can already be done by subsequent calls to clear() and append(), but you're right, it might be usefull as a separate method

ivirabyan commented 9 years ago

I just don't know, is it better to pass marked up value, or just an object, like this $('.mentions').mentionsInput('setValue', 'Hey ', {name: 'alex', uid: 'user:1'})

pergardebrink commented 9 years ago

Yes, I did thought about using clear() and then append() but it didn't let me pass marked up value in the same format as getValue() returns, so then I would need to first parse the value myself before appending..

My use case needed me to be able to set the markup directly as a string (I am using it with KnockoutJS and needed to set the value directly from my custom binding).

pergardebrink commented 9 years ago

Maybe there could be different versions for getting/setting the value in either string format or in marked up format as an object. Something like this (don't know about the names though)

$('.mentions').mentionsInput('setValue', 'Hey @[alex](user:1)');
$('.mentions').mentionsInput('setMarkup', [ 'Hey ', {name: 'alex', uid: 'user:1' ]);

$('.mentions').mentionsInput('getValue'); // Returns 'Hey @[alex](user:1)'
$('.mentions').mentionsInput('getMarkup'); // Returns a javascript array in marked up format: [ 'Hey ', {name: 'alex', uid: 'user:1' ]
pergardebrink commented 9 years ago

Hmm. Do all my commits to my fork automatically show up here? That wasn't my intention really since I'm just getting my fork up to sync with my customized version I have.. (although the minLength property is something I was missing. I needed to show all available options without needing to know the first character)

ivirabyan commented 9 years ago

This makes sense. I finally decided to throw away the append() method, and added setValue() method, which supports both ways of passing mentions, marked up, and object-style mentions. $textarea.mentionsInput('setValue', 'Hello, @[Alex](user:123)'); $textarea.mentionsInput('setValue', 'Hello, ', {name: 'alex', uid: 'user:123'});

ivirabyan commented 9 years ago

Yes, all your commits are automatically appended to pull request. You'd better create a branch for every pull request you want to make.