Chapters
48 Chapters
|
5:05:52
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
36.
Skipping Ajax: Sending JSON Straight to Vue
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This course is also built to work with Vue 3!
What JavaScript libraries does this tutorial use?
// package.json
{
"devDependencies": {
"@symfony/webpack-encore": "^0.30.0", // 0.30.2
"axios": "^0.19.2", // 0.19.2
"bootstrap": "^4.4.1", // 4.5.0
"core-js": "^3.0.0", // 3.6.5
"eslint": "^6.7.2", // 6.8.0
"eslint-config-airbnb-base": "^14.0.0", // 14.1.0
"eslint-plugin-import": "^2.19.1", // 2.20.2
"eslint-plugin-vue": "^6.0.1", // 6.2.2
"regenerator-runtime": "^0.13.2", // 0.13.5
"sass": "^1.29.0", // 1.29.0
"sass-loader": "^8.0.0", // 8.0.2
"vue": "^2.6.11", // 2.6.11
"vue-loader": "^15.9.1", // 15.9.2
"vue-template-compiler": "^2.6.11", // 2.6.11
"webpack-notifier": "^1.6.0" // 1.8.0
}
}
10 Comments
Hey hi!, I need pagination info,
how can it be done?
ty!
Hey diegoinaui!
That is an excellent question! And one that I have thought about before... solved... and forgotten how 🤦♂️. I believe what you need to do is this:
A) Create your query. Then use that to create a Doctrine paginator object. That's an instance of
Doctrine\ORM\Tools\Pagination\Paginator.B) Use THAT to create an ApiPlatform Paginator object - that is an instance of
ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator.Finally, you will pass the Paginator object from part B to the serialize filter.
To help a bit more, if I'm reading the code correctly, here is where these objects are created in API Platform: https://github.com/api-platform/core/blob/c81eaae600d9472f65df7d4d8bf41145b06390b8/src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php#L160-L185 - note that the
DoctrineOrmPaginatorclass in there is an alias to the Paginator class from part (A).Let me know if that helps!
Cheers!
Hi Ryan! This question interests me as well.
But do you think it would also be possible to do it this way:
1. I am using your (beautiful) KnpPaginator bundle which will contain the data (not serialized), the pagination info.
2. I serialize the category data in some way (from the controller by modifying the data that will be stored in the knpPaginator paginator if possible).
3. I pass the object to Twig and put it in global to be able to access it from the JS.
What do you think of this alternative? (Or maybe it should get some fixes?)
Hey Kiuega,
I'm not sure about KnpPaginator, but I think a good idea to try. Doctrine Paginator is just a bit more lightweight and low level, Knp Paginator has more overhead, but if you get used to it - why not to try it instead?
> 3. I pass the object to Twig and put it in global to be able to access it from the JS.
Depends on how your JS code works, but sounds good to me too, simple and working solution.
Cheers!
Hi there!
I really don't like this inline script tag nor the global variables. Is there a reason why we use that instead of some data attributes on the body or div#app tags?
Thanks
Hey @Julien!
No reason at all :). This is a total personal preference - feel free to use data-attributes on body. They take 1 extra line of code to read them (no bid deal) and be careful with types (everything will be a string), but that’s it. I *do* like that data- attributes can be more localized - you can even put them in the div that you’re rendering into and read them from that :).
Cheers!
Hello !
Is there a way to solve the loading issue when your API is on another server ? WEB-CLIENT on https://myapp.com and API on https://api.myapp.com for example.
Hi again! :p
What issue are we talking about exactly? How would you be loading the data? If you're referring to a possible CORS issue, yes, there is a way to fix that with proper headers.
Here's an article about it: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Talking about the categories loading side effect of the AJAX call. If we have the API and the frontend on 2 different servers this is not solvable by pushing directly inside Twig. Or maybe by doing a call to the API in PHP and sending data through Twig. Anyway that's not a big issue for now :)
That's right. Sending data directly from the server to twig makes sense if you have your API in the same server as your Symfony App. Alternatively, you may be able to perform calls to the API directly from the back end server, so they are ready to be injected as data inside of your component on page load, but this is a far more complex scenario!
"Houston: no signs of life"
Start the conversation!