ki.js icon indicating copy to clipboard operation
ki.js copied to clipboard

Creating a working $(foo).find() method

Open tarjei opened this issue 8 years ago • 3 comments

Hi, thanks for creating ki.js!

There are two usecases of the constructor that seem to not work so well.

I'm trying to implement a simple find() method but I cannot get it to work correctly. I think this might be because the constructor does something vonky, with NodeList objects, but I'm not sure.

Here's my implementation: ``js` $.prototype.find = function (selector) { var res = [] this.each(function (element) { res = res.concat(new $(element.querySelectorAll(selector))) }) return res }


There are several issues with the above code - the array needs to be flattened across multiple calls to `querySelectorAll` amongst other things.

What I am wondering is if it is possible to implement this method as an extension or if I need access to the internal c variable and thus the method must be within the main ki.js code.

Also, there are two common usecases that I find missing from the constructor which are `$(Element)` - I.e. creating a $ object wrapping an existing element and `$([Element])` which would solve part of the prolem with implementing the find method above.

Last but not the least, I'm wondering about the e variable in the i function:
 
```js
   /*
   * init function (internal use)
   * a = selector, dom element or function
   */
  function i(a) {
    c.push.apply(this, a && a.nodeType ? [a] : '' + a === a ? b.querySelectorAll(a) : e)
  }

  /*
   * $ main function
   * a = css selector, dom object, or function
   * http://www.dustindiaz.com/smallest-domready-ever
   * returns instance or executes function on ready
   */
  $ = function (a) {
    return /^f/.test(typeof a) ? /c/.test(b.readyState) ? a() : $(b).on('DOMContentLoaded', a) : new i(a)
  }

Is the e correct here? What is the expected value of e?

Kind regards, Tarjei

tarjei avatar Mar 23 '17 06:03 tarjei

hi @tarjei could this be solved by https://github.com/dciccale/ki.js/pull/24 ?

dciccale avatar Nov 30 '19 11:11 dciccale

I haven't tested it, but it might :)

tarjei avatar Nov 30 '19 15:11 tarjei

This will work when #24 is merged, after a bug is fixed.

boehs avatar Nov 03 '21 17:11 boehs