LeaVerou / bliss

Blissful JavaScript
http://blissfuljs.com
MIT License
2.39k stars 103 forks source link

Element getters? #175

Open fractaledmind opened 8 years ago

fractaledmind commented 8 years ago

Lea,

Once again, a great library. Thank you so much for this! :)

I was wondering if you would be amenable to having the functions defined under $.setProps be written to work as both "getters" and "setters"? By this I simply mean that if you pass a param to the function (e.g. Bliss.properties(subject, props) or subject._.properties(props)) it functions as a "setter", but if you simply call the function without a param (e.g. Bliss.properties(subject) or subject._.properties()) it functions as a "getter".

My initial implementation is fairly rudimentary, but it has worked for the use-cases I have. I am simply checking for the existence of the single param and splitting the functionality into two code paths accordingly. To take properties as a simple example:

properties: function (val) {
    if(val) {
      $.extend(this, val);
    }
    else {
      return $.extend({}, this);
    }
}

Obviously, not every function in $.setProps can have both a "getter" and a "setter", but plenty could. And I think the functionality would be quite helpful (I personally need "getters" for attributes and properties).

Once again, thank you for all of your hard work, stephen

LeaVerou commented 8 years ago

Not sure about this. How often do people need to get ALL properties or ALL attributes from an element? It seems like a special case to me, and as you pointed out, it can already be done in 1 line (for attributes just do $$(this.attributes).map(attribute => attribute.name)). We should be mindful of bloat, otherwise we'll end up with yet another bloated library. What do others think?

LeaVerou commented 8 years ago

That said, Bliss should be extensible enough to allow third party devs to do this via plugins. We already have a hooks system, though it cannot intercept the return value, so maybe we should work on that.