Skip to content

Instantly share code, notes, and snippets.

@sourcebits-robertbiggs
Created September 20, 2013 16:38
Show Gist options
  • Save sourcebits-robertbiggs/6640321 to your computer and use it in GitHub Desktop.
Save sourcebits-robertbiggs/6640321 to your computer and use it in GitHub Desktop.
A plugin to normal hasClass is,not and has to work like ChocolateChipJS: hasClass, is, isnt, has and hasnt. This is for implementing support for jQuery with the ChocolateChip-UI framework. With these, the methods will always return the filtered results so that you can perform operations on them directly: $('li').hasClass('selected').css('color',…
$.fn.extend({
hazClass : function ( className ) {
var ret = $();
this.each(function(idx, ctx) {
if ($(ctx).hasClass(className)) {
ret.push(ctx);
}
});
return ret;
},
iz : function ( selector ) {
var ret = $();
this.each(function(idx, ctx) {
if ($(ctx).is(selector)) {
ret.push(ctx);
}
});
return ret;
},
iznt : function ( selector ) {
return this.not(selector);
},
haz : function ( selector ) {
return this.has(selector);
},
haznt : function ( selector ) {
var ret = $();
this.each(function(idx, ctx) {
if (!$(ctx).has(selector)[0]) {
ret.push(ctx);
}
});
return ret;
}
});
@sourcebits-robertbiggs
Copy link
Author

ancestor vs closest. I have to say, I could never understand why they named it closest. It not about the closest descendant ever, only an ancestor.

Good one on the hazAttr. I forgot.
I'm not sure that dataset for jQuery is necessary. For jQuery you'd just use $(selector).data('goto') and it will return the data-goto attribute's value.
ChocolateChipJS uses data only for the cache and uses dataset for the data attribute stuff. I could merge them in ChocolateChipJS. I've thought about it. But like having them separated for now.

@notacouch
Copy link

yep. But if they named it ancestor... that would've made sense!

Yeah I'm aware jquery's data may refer to data-* attributes, just didn't want to replace original chocolatechip code. I'm still coding with chui in mind, in future versions i may not need jQuery, want to retain the distinction in my head, this helps. Also I like the distinction between data store and an element's attribute, the ambiguity has caused problems in the past.

What do you think of a hazntAttr/hazntClass? better to just use iznt? reads better/easier to follow imo.

this isn't comprehensive or anything, just literally what I needed to make the switch. There are a few more but I don't think they warrant posting here. $.form2JSON for sure was a welcome addition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment