arvgta / ajaxify

Ajaxify - An Ajax Plugin
https://4nf.org/
275 stars 124 forks source link

Enrich $.rq #183

Closed arvgta closed 4 years ago

arvgta commented 4 years ago

Purpose: rq stands for "request"

This sub-plugin is at the heart of Ajaxify. The idea is to collect data pertaining to the request centrally and flexibily rather than passing it around. It already has succeeded in relieving the client code on many occasions. Furthermore, the data can be persistent, potentially even across page loads.

I would like to enrich rq, simplify calls for clients and ideally downsize, concerning the following code:

    if(o === "v") { //validate value passed in "p", which is expected to be a click event value - also performs "i" afterwards
        if(!p) return false; //ensure data
        e = p; //store event internally
        c = e.currentTarget; //extract currentTarget
        h = c.href; //extract href
        if(!_internal(h)) return false; //if not internal -> report failure
        o = "i"; //continue with "i"
    }

    if(o === "i") { //initialise request defaults and return "c" (currentTarget)
        ispost = false; //GET assumed
        data = null; //reset data
        push = true; //assume we want to push URL to the History API
        can = false; //reset can (canonical URL)
        return c; //return "c" (currentTarget)
    }

    if(o === "h") { // Access href hard
        if(p) {
            e = 0;  // Reset e
            h = p;  // Poke in href hard
        }

        return h; //href
    }

From here on:

  1. "v" : validate event
  2. "i" : initialise defaults
  3. "h" : access href

... will be referred to as "steps"...


Problems: things I don't like about the current code:

  1. If several calls of rq are made sequentially, they cannot be performed in one go.
  2. Step "i" always returns c (currentTarget), which is fetched twice at the moment but never needed in practice (client calls always want the href and extract it "by foot" instead, which is not elegant at all).
  3. Step "h" has to be called separately, in order to retrieve the href, even if it could be done in one pass.

There are principally two very different solutions:

  1. Make $.rq more self-managing, so that it renders the value with the maximum conveniency in all cases.
  2. Allow the client to specify several steps at once - something like:
    var h = $.rq("vih", ev) //perform all three steps and return href in one pass

In the second variant, all steps would be processed sequentially as follows:

  1. "v": The only one to handle p
  2. "i" : Without processing p
  3. "h" : Finally returns href (in one go..)

Question:

1. Does the second variant go down well with object-oriented paradigms, or is it known to defeat its purpose?

edito commented 4 years ago

From my perspective seems that the first option would be better. For second variant we'll probably need giant for/foreach loop and names of getters/setters would be limited to one character possibly. But even that is not much of an issue because, as you said, we have whole alphabet doubled (case sensitive checks), and big spectrum of symbols. Letter case can be even used to determine the return of the call or some additional instructions. For example:

$.rq("v", ev); // will do just "v", but
$.rq("V", ev); // will do "vi".

To make it more clear, here is working example:

if(o.toUpperCase() === "V") { //validate value passed in "p", which is expected to be a click event value - also performs "i" afterwards
    if(!p) return false; //ensure data
    e = p; //store event internally
    c = e.currentTarget; //extract currentTarget
    h = c.href; //extract href
    if(!_internal(h)) return false; //if not internal -> report failure
    if (o === "V") o = "i"; //continue with "i"
}

What you think?

arvgta commented 4 years ago

Thanks Edin! I reallly like your suggestion with the case sensitviity to distinguish between setters and getters, or different handlers on the same piece of data!

I've tweaked canonical URL handling accordingly...

Regarding your working example:

  1. I would use: if(o.toLowerCase() === "v") //softer on the eye
  2. The present algorithm always calls "i" after "v", is why your code is not the perfect example.

Apart from that I don't want to throw out the idea of performing multiple steps at once just yet... Don't think, that it will be much more complex than a String.indexOf(), only that it might have to handle case additionally. Yes, I would opt for each step to be indicated by a single character, but the standard method for "jumping" into the desired steps could be handled by the pO() function itself... Any combination of steps could be specified by the user.

After all, what you proposed is a cooperative method between the client and $.rq anyway, so we might take this cooperation one step further...

arvgta commented 4 years ago

I have only performed the following changes to $.rq:

    if(o === "v") { //validate value passed in "p", which is expected to be a click event value - also performs "i" afterwards
        if(!p) return false; //ensure data
        e = p; //store event internally
        h = typeof e !== "string" ? e.currentTarget.href || e.originalEvent.state.url : e; //extract href
        if(!_internal(h)) return false; //if not internal -> report failure
        o = "i"; //continue with "i"
    }

    if(o === "i") { //initialise request defaults and return "c" (currentTarget)
        ispost = false; //GET assumed
        data = null; //reset data
        push = true; //assume we want to push URL to the History API
        can = false; //reset can (canonical URL)
        return h; //return "h" (href)
    }

These two lines have been changed:

h = typeof e !== "string" ? e.currentTarget.href || e.originalEvent.state.url : e; //extract href
        return h; //return "h" (href)

The calling functions (clients) have been simpified accordingly... For example, it is now possible to do the following in one single step:

var h = $.rq("v", ev);  //validate event and return the href
arvgta commented 4 years ago

Tested against:

...successfully, thus closing