How to load AJAX only when app loads the first time

Can you please advise, where to place an ajax request, in order for it to be called only once - when the app is first opened?
Tried to hook it to “Init” (see sample below) but receive “$$.ajax is not a function”.

var app = new Framework7({
on: {
init: function () {
$$.ajax ({
//AJAX call

I also tried to put it this way, but it also doesn’t work.

$$(document).on(‘page:init’, ‘.page[data-name=“home”]’, function (e, page) {
//AJAX call

There is no $$.ajax in v2, check docs

I do understand that app.request is the correct way of calling it, however, app.request upon app:init event returns “app is undefined”, which obviously means that app is not yet defined at that time, when init event is fired.
So I tried with $$.ajax, instead.

Can you please point me to the right direction to resolve this?

this in app event listener will the app instance, so this.request(…)

Or request is available on Framework7 constructor so Framework7.request will also work

Thank you so much, I was able to get it working.

One additional question, if I may ask - is it possible to define a global APP variable within the app:init event?
Tried both - = ‘foo’ and = ‘foo’, but both return this global variable as undefined.

I don’t know if that’s the best approach, but got this working by assining the instance context to a variable before the this.request call, so I am able to access this after the success.

Прошу прощения, а есть ли минимальный рабочий пример, как получить через AJAX JSON результаты на главное окно? Чтобы уже с этого начать строить свое первое приложение.
P.S. Кто бы мог помочь за небольшое вознаграждение?

Как получить данные по AJAX:

Как устроена маршрутизация:

А вообще начинайте с первой страницы документации: и вперед. Если после прочтения останутся конкретные вопросы, то задавайте.

1 Like

Что имеется ввиду под “главным окном”? Главная страница? Можно также грузить ее через роут как компонент