JQuery Posts

How to use laravel routes with Javascript / JQuery ?

Generally, we can use laravel routes into its blade files, but what do we have to do when we want to use routes in javascript? is that possible to use laravel routes into javascript?

Yes, now you can use laravel routes into laravel, thanks to Tighten/Ziggi package.

In this tutorial, we will learn how we can use laravel routes into javascript, so let's get started.

Install the package

composer require tightenco/ziggy

Update your main layout file

Add the @routes Blade directive to your main layout (before your application's JavaScript), and the route() helper function will now be available globally!

E.g (app.blade.php)

... ... @routes .. ..

Usage

// routes/web.php

Route::get('users', fn (Request $request) => /* ... */)->name('users.index');

// app.js

route('users.index'); // 'https://url.test/users'

So this is how its works, so simple right :)

You can get more information about this package from here

Kepp connected to us to get the latest laravel information.

January 03, 20223 minutesVishal RibdiyaVishal Ribdiya
Show saved annotations from database in document using PDFTron

Here we will learn how to import annotations saved in the database using PDFTron.

In my last blog, we have learned how to save annotations in the database.

Events for import annotation

At the time of document load, we will get all annotations saved in the database using AJAX call and then we'll import that annotation. Now the question is if we import annotation then that will be drawn and again annotation changed event will fire and again annotation will be saved (as I say in my last blog), so this will become an infinite flow, but we can overcome this problem by checking if an annotation is imported or not. If an annotation is not imported then only we'll save annotation in the database process otherwise we'll ignore it.

When we draw any annotation, the "annotationChanged" event will be fired, and check if it is an imported annotation, then we can ignore it(eg, save annotation process).

Here is an example of how to import annotations from the database.

Example

WebViewer({
    path: 'path_to_the_PDFTron_'lib'_folder_on_your_server',
    css: 'webviewer_css',
    licenseKey: 'YOUR_PDF_TRON_LICENSE_KEY',
    initialDoc: 'YOUR_FILE URL' //url of a file to load
}, document.getElementById('viewer'))
    .then(function (instance) {
        let docViewer = instance.docViewer;
        let annotManager = instance.annotManager;

        annotManager.on('annotationChanged', (annots, action, e) => {
            //if annotation is imported we'll return
            if (e.imported) return;
            //when document will loaded we'll get annotations fro db
            docViewer.on('documentLoaded', function () {
                $.ajax({
                    url: `URL_TO_SAVE_ANNOTATION`,
                    type: 'GET',
                    success: function (result) {
                        if (result.success) {
                            result.data.forEach(annotationObj => {

      annotManager.importAnnotations(annotationObj.annotation);
                            });
                        }
                    },
                    error: function (result) {
                        console.log(result);
                    }
                });
            });
        });
    });
December 28, 20201 minuteMonika VaghasiyaMonika Vaghasiya