This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

eddyloewen
eddyloewen

I'm trying to reference a variable inside a Component class from an Ajax call. I would like to pass it back to a partial.

$this->page['variable'] = $this->mVariable;

The second $this seems to be the problem. Is it not possible to reference variables inside a Component class from Ajax calls?

eddyloewen
eddyloewen

I would like to retrieve and modify a variable through different and independent ajax calls

Blackpig Creative
Blackpig Creative

Can yo explain in more detail or give code example?

eddyloewen
eddyloewen

I'm working on a component which holds some data.

class Tariff extends ComponentBase
{
    public $mData;
}

I would like to modify this data via ajax calls and return it back to a partial for example.

Modify:

public onModifyData()
{
     $data = post('data');
     $this->mData['category_a'] = $data;
}

And retrieve it afterwards through a different ajax call:

public onRetrieveData()
{
     $category = post('category');
     $this->page['dataForCategory'] = $this->mData[$category];
}

<a href="#" data-request="onRetrieveData" data-request-data="category: 'category_a'" data-request-update="'{{ __SELF__ }}::_modal': '#modal_content'" data-toggle="modal" data-target="#modal">Show modal</a>

The problem is - it seems that I can't reference $this->mData in ajax calls as it doesn't return any data to the partials. I have lots of modal views to modify my data and I don't want to render all of them at once (at the beginning) as it slows down the site a lot. So I would like to render them only if I need them. Therefore I need to read some data (specific to the modal view) from the overall data - pass it to the view - modify it - send it back via ajax to the $this->mData

But it doesn't work the way I think it would

Last updated

Blackpig Creative
Blackpig Creative

Is the data being set? can you dd() the values in both the ajax requests? How are you rendering the modal? You can return a rendered partial and pass it variables in an array as push

http://octobercms.com/docs/cms/ajax#pushing-content-updates

eddyloewen
eddyloewen

I set the data in the public function onRun(){} method. When I dd() there - the data is set.

Apparently I cannot dd() in the ajax functions, right? Nothing happens when I try to do that.

I'm able to render partials and also pass data to them like explained in that link. But the problem still is that I cannot get any data from the variable stored in $this->mData in die Component class

eddyloewen
eddyloewen

It seems that I don't have the same context when calling methods via ajax.

Any other idea how to work with data across multiple and different ajax calls?

Harti
Harti

I know, this is ancient - having a similar issue though and grasping straws. Did you find a solution @eddyloewen?

eddyloewen
eddyloewen

@harti Sorry - I could't find a solution...

Troiscent
Troiscent

I have a similar issue, I think it's a bug. I posted an issue to the github issue queue :

https://github.com/octobercms/october/issues/3495

1-10 of 10

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.