How do I run the script after loading the page

1

I'm having a question about framework7, how do I run the scripts after dynamically loading the page, example:

mainView.router.loadPage("pagina.html)


[script que registram eventos nos botoes da pagina acima]

$("#capturarFoto").unbind().bind("click",function () {
    navigator.camera.getPicture(onSuccessCapturaImagem, onFailCapturaImagem, 
 {
        quality: 50,
        correctOrientation: true,
        encodingType: navigator.camera.EncodingType.JPEG,
        destinationType: navigator.camera.DestinationType.FILE_URI
    });
});

$("#procurarFoto").unbind().bind("click",function () {
    navigator.camera.getPicture(onSuccessCapturaImagem, onFailCapturaImagem,
        {
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
            encodingType: navigator.camera.EncodingType.JPEG,
            correctOrientation: true
        }
    );
});

When I do this it does not log events on the button, does anyone know why?

    
asked by anonymous 25.09.2017 / 15:12

3 answers

4

You can use the pageInit event, link

$(document).on('pageInit', function (e) {
    //as funções de bind
    $("#capturarFoto").unbind().bind()...
});
    
25.09.2017 / 15:25
3

The Framework7 itself has a way of running functions every time you access a page.

app.onPageInit('pagina', function (page) {
  // Sua função...
})

app is the assignment of framework7 , in general they are set to app or MyApp . The name of pagina passed in onPageInit is set right in your file. Ex:

<div class="page" data-page="pagina">

</div>
    
25.09.2017 / 15:37
0

As you are using jQuery / Zepto you can use the event delegate and instead of running at all times would just do so:

$(document).on("click", "#capturarFoto", function () {
    navigator.camera.getPicture(onSuccessCapturaImagem, onFailCapturaImagem, {
        quality: 50,
        correctOrientation: true,
        encodingType: navigator.camera.EncodingType.JPEG,
        destinationType: navigator.camera.DestinationType.FILE_URI
    });
});

$(document).on("click", "#procurarFoto", function () {
    navigator.camera.getPicture(onSuccessCapturaImagem, onFailCapturaImagem, {
        destinationType: navigator.camera.DestinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
        encodingType: navigator.camera.EncodingType.JPEG,
        correctOrientation: true
    });
});

This script should be run only once.

    
25.09.2017 / 15:31