Giter Site home page Giter Site logo

Comments (6)

feuzeu avatar feuzeu commented on July 19, 2024

Hi,

I apologize for the delay, and I hope this will still help.

You can define a callback which will add a default response if none is given in your user functions.
https://www.jaxon-php.org/docs/responses/callbacks.html

jaxon()->register(Jaxon::PROCESSING_EVENT, Jaxon::PROCESSING_EVENT_AFTER, 'setDefaultResponse');
function setDefaultResponse()
{
    $response = jaxon()->newResponse();
    // You can use the global response provided by the library
    // $response = jaxon()->getResponse();

    // Set the default response content
    ...

    // Append your response to the request response
    jaxon()->getResponseManager()->append($response);
}

from jaxon-core.

vita10gy avatar vita10gy commented on July 19, 2024

Thanks for the suggestion. I added that but the fatal error happens before it gets to running setDefaultResponse. If I try PROCESSING_EVENT_BEFORE it gets there, but seems to make no difference in the fatal error above.

Also just to be clear I don't know ahead of time if the function has a response returned since I'm trying to make a backward compatible replacement that just works without touching a bunch of sites.

Likewise, I wouldn't want to do something that just swapped not needing to specify a response object by default with an approach that overrode when there WAS a response returned, if that's where this is headed.

$this->ajax_class = new \Jaxon\Jaxon();
 $this->ajax_class->setOption("core.prefix.function","xajax_");
 $this->ajax_class->setOption("core.request.uri",$_SERVER["REQUEST_URI"]);
//the line below didn't exist before your suggestion
 $this->ajax_class->register(\Jaxon\Jaxon::PROCESSING_EVENT, \Jaxon\Jaxon::PROCESSING_EVENT_BEFORE, 'setDefaultResponse');
        foreach($this->functions as $f)
        {
          $this->ajax_class->register(\Jaxon\Jaxon::USER_FUNCTION, $f);  
        }
        
        $this->ajax_class->processRequest();

and then as an example of what $this->functions could contain, here would be one with no response

function save_sorting($array)
  {
    $index = 1;
    foreach($array as $id)
    {
      $temp = new Page($id);
      $menu = $temp->getMenu();
      if($menu->exists())
	{
		$menu->setSortOrder($index);
	}
      $index++;
    }
  }

This results in a fatal error. However when I return a new Jaxon\Response\Response in save_sorting all is well.

Of course I could be missing the boat completely on something.

from jaxon-core.

feuzeu avatar feuzeu commented on July 19, 2024

Hi,
You were right, until the latest https://github.com/jaxon-php/jaxon-core/releases/tag/v2.2.4 release I just shipped, it was not possible in a user function not to return a response.
With this release, you can define your event handler as follow.

function setDefaultResponse()
{
    $manager = jaxon()->getResponseManager();
    if($manager->hasNoResponse())
    {
        $response = jaxon()->newResponse();
        // You can use the global response provided by the library
        // $response = jaxon()->getResponse();

        // Set the default response content
        ...

        // Append your response to the request response
        $manager->append($response);
}

from jaxon-core.

vita10gy avatar vita10gy commented on July 19, 2024

Awesome, thanks. Do we have to set a default, or is just doing nothing nothing cool too?

from jaxon-core.

feuzeu avatar feuzeu commented on July 19, 2024

You're welcome.
You have to set a default, since the request actually needs a response to be returned to the client.

from jaxon-core.

feuzeu avatar feuzeu commented on July 19, 2024

Do you think it is a good idea if Jaxon returns the global response object when no response is defined either in the user function or in the after processing callback?

from jaxon-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.