Closed bebraw closed 9 years ago
Yes, useful. I'll provide an option to ignore dups. Thanks
@bebraw I've decided the best way to handle this is at model level. You can provide option in the model ctor to disallow duplicates, and you can provide your custom function as well. Check out the demo and let me know. Thanks
@VikramN Looks like a nice API. You can simplify this if you want, though. Demo:
// Current API
var tags1 = new TagCollection({
duplicates : {
// Dups are allowed by default
allow : false,
// If not provided, we use ===
// tags will be array of { tag: , key: }
contains : function(item, tags) {
var lTag = item.toLowerCase();
for(var i =0; i < tags.length; i++) {
if(tags[i] && tags[i].tag.toLowerCase() === lTag){
return true;
}
}
return false;
}
}
});
// Improved API
var tags1 = new TagCollection({
// by default we allow every tag. you can check against duplicates etc. here
allowTag: function(item, tags) {
var lTag = item.toLowerCase();
for(var i =0; i < tags.length; i++) {
if(tags[i] && tags[i].tag.toLowerCase() === lTag){
return true;
}
}
return false;
}
});
This will eliminate the flag and keeps things simpler. You can provide a couple of helpers with the library for common tasks (ie. detecting duplicates). Then you would just do allowTag: ReTag.disallowDuplicates
and so on.
Hey. Thanks for the suggestion. I've added generic onAdd and onDelete options. Let me know what you think.
@VikramN Looking good. :+1:
Ie. if I hit "foobar" twice, I end up with two "foobar" tags. That's not very convenient. :)