Why do I have to use a chain of return after a js()->reload?

I have a page and a view. The view is added to the page. On the page are 2 buttons, setting a value in the view. After I press either of the 2 buttons, I want the view to update itself to reflect the value set in the view.`

This is the code of the page:

class page_setpet extends Page {
        function init(){
            parent::init();
            $vm=$this->add('View_Viewpet');

            $button=$this->add('Button');
            $button->set('Dog');
            $button->onClick(function($button) use ($vm) {
                return $vm->setName('Dog');
            });

            $button=$this->add('Button');
            $button->set('Cat');
            $button->onClick(function($button) use ($vm) {
                return $vm->setName('Cat');
            });
    }
}

And this of the view:

class View_Viewpet extends View {
    public $pname;

    function init() {
        parent::init();
        $this->pname=$this->app->recall('pet_name', 'Default');
        $this->addClass('atk-row atk-box atk-col-4 atk-move-center');
        $this->add('H2')->set('Pname: '. $this->pname);
    }

    function setName ($n) {
        $this->pname = $n;
        $this->app->memorize('pet_name', $this->pname);
        return $this->js()->reload();
    }
}

Now in Vieuw_Viewpet::setName() I use a return. Also in the onClick functions of the 2 buttons.

If I discard either return value (that is, the return value of js()->reload()), the view does not update anymore.

I can’t find anything in the documentation about returning the value of js()->reload() to the first caller. I only found out by carefully imitating one of Romans’ presentations. It is fine that I know this, but how do I understand this for future code?

Why is this so?

jlinkels

"$button->on(‘Click’ … " is only good if you need to do some serverside before executing some js.

in ATK this on function gives you ability to run some PHP before your JS to execute. Since it is made to run JS it must return a JS

I would have done it as following

$vm= $this->add(‘View_Viewpet’,[‘pname’=>$_GET[‘pname’]]);
$button_dog->js(‘click’,$vm->js()->reload([‘pname’=>‘Dog’]);
$button_cat->js(‘click’,$vm->js()->reload([‘pname’=>‘Cat’]);

This way I just say to reload ViewPet with providing some info direclty at client side. In your case there is two server trips, 1. To run onClick and then to reload view. And you also need to memorize to keep track of value between two server trips.

My answer may not fit your case but it will surely help you understanding ATK more :slight_smile:

Hi.

This inspired me to create a small example.

http://sink.agiletoolkit.org/js/dog-n-cat

and code is here:

@gowravvishwakarma: Thanks for the explanation and the suggestion for the alternative.

in ATK this on function gives you ability to run some PHP before your JS
to execute. Since it is made to run JS it must return a JS

I am not sure I understand. Do you mean by including return statements I force PHP to include the JS code? Rephrased: if there is no return PHP doesn’t see the need to include the JS code?

I used your suggested example for refreshing another part on the page, not included in the example I posted. Indeed that works fine. I just was not aware of the parameter passing using $_GET. If that is in the documentation, I missed it.

@romaninsh That is an excellent example, thanks for making it a complete example. Simple and basic examples like this hugely improve the ATK4 documentation and code snippets. Often the documentation jumps to too complicated and advanced code. BTW, in my original post I copied the code from your Shopping Basket tutorial. Since my test database is with Pets, I used Dogs and Cats instead of Shoes and Hats. I excluded the model stuff from the MWE.

jlinkels

@jlinkels, you know that I’m not getting paid for working on ATK, it’s a free framework, right ? all the tests and documents I publish i do it in my free time.

I am OK if others use those examples, and I ask very little in return:

I can write more examples, but i need to know what others struggle with, giving me good suggestions on what to implement so that it is beneficial to others.

So if anything is unclear in the documentation, please make request for new example and I’ll add either me or someone else will add it.

Some older tutorials can be cleaned up too. There are a lot of content and I can’t manage everything myself, so some stuff is outdated.

@jlinkels,
you know that I’m not getting paid for working on ATK, it’s a free
framework, right ? all the tests and documents I publish i do it in my
free time.

@romaninsh Maybe I did not express myself well. Of course I understand that what you do is a great gift to the community. My last post was meant as a compliment to you. I was not complaining about lack of documentation. On the contrary, I thanked you for providing this simple example.

Only my first post in this forum was something of a complaint but that was in pure frustration because I could see the power of ATK, but was not able to use it.

Sure I will try to give suggestions for examples, ask questions which could produce useful answers for others, file bug reports, etc. Anything that could be helpful for the community. Although while I am on this level of experience my contribution might be minimal, and I might ask the wrong and stupid questions due to a lack of understanding.

You have been very helpful to me, almost being a one-to-one tutor. Sorry again if I gave you the wrong impression.

jlinkels

1 Like

@jlinkels Hi, where can I find the sample shopping basket. I would like to learn how ATK implements a shopping cart. I had built a cart based on $_SESSION and MySQL in php and would like to learn more in ATK4.3. Thanks.