Pass list of objects between files

Asked

Viewed 403 times

3

How to pass a list of objects through two Javascript files?

I have a list:

var elementsList = [];

And this list is populated with N objects:

var oElement = {
    elem: "",
    emin: 0.0000,
    emax: 0.0000
}

I need to move all this to a modal window:

var oElem = {
            "id": "FCADELEM",
            "data-height": "558",
            "data-width": "320",
            "data-name": "Elementos Químicos",
            "data-url": "FCADELEM.aspx?returnToForm=ifr_FCADQUAL",
            "data-module": "Elementos Químicos",
            "attr": function (param) {
                return this[param];
            }
        }
        parent.createDialog(oElem);

How can I pass this list of objects to the other window without using C#?

ADDITIONAL INFORMATION

  • The two windows in question are Modal iframes from the jQuery library.

  • Unable to store the data in the js of the parent page'.

  • Both windows are from the same domain.

  • I don’t understand your problem. Can you explain better? The array and the object are in different files/pages? at different times?

  • And basically, I open a modal window by clicking a button, Parent.createDialog(oElem); creates the modal, and I need this list within the modal JS, which I should open.

  • 1

    Your modal is an iframe? if not, then everything is available. You may have to adjust the scope, but it seems simple to solve. Another question. What is the relationship between the two? For example, the 1st el of the array has to do with which part of the object?

  • The window where the data comes from is also an iframe, it is not possible to store on the parent page. So, theoretically, it’s like it’s from one page to another.

  • Ok, and iframe is on the same domain? and by the way, use some library like jQuery or Mootools?

  • 1

    William, with the details missing from the question it will be possible to answer with the solution. You can add this information to the question also to make it more complete.

  • Yes, in the same domain, use jQuery.

  • William, I put an answer. I don’t know which modal you use, if it’s not the jQuery UI say which one I can adjust the answer to.

Show 3 more comments

2 answers

2

To access for example a function within an iframe in the same domain you can do so:

var iFrame = $('#myIframe')[0].contentWindow;
iFrame.receberDadosExternos('Olá!!');

This implies that there is a function within the iframe with that name and that it is in the global scope.

Example:

function receberDadosExternos(dados) {
    alert(dados);
}

Live example

In the jsFiddle example I put on top use this code that runs only when the dialog opens:

$("#dialog").dialog({
    autoOpen: false,
    modal: true,
    height: 600,
    open: function (ev, ui) {
        var iFrame = $('#myIframe')[0].contentWindow;
        iFrame.receberDadosExternos('Olá!!');
    }
});

But you can call the function whenever you want and pass whatever you want into it.

In case you want otherwise, ie calling a function from within the iframe to the "mother" window can use simple javascript, ie:

window.parent.nomeDaFuncao();

1


Thanks for the collaboration Sergio, helped a lot to the unfolding of the issue, but I had to make some adaptations, because the list of objects is a little more complex than a string, for example..

The resolution was as follows::

I thought of using Postmessage, but for that I would need the modal iframe to be open, first step then create the modal. In the newly created modal, it returns to the previous iframe, and collects the object. We call the function getaway() on onload.

function getItem() {
    try {
        var returnToUrl = "FCADQUAL.aspx";
        var msgAddr = window.location.protocol + "//" + window.location.host + returnToUrl;
        var o = parent.document.getElementById("ifr_FCADQUAL");
        o.contentWindow.postMessage("100;0", msgAddr);
    } catch (e) {
        Save Exception();
    }
}

In the previous iframe then, you receive the post merge, and add the object to the parameter, and send it back:

window.addEventListener("message", receiveMessage, false);

function receiveMessage(e) {
    try {
        //url que verifica se post message é valido
        var url = window.location.protocol + "//" + window.location.host
        //testa se origem do post é do mesmo dominio
        if (e.origin !== url)
            return;
            var sData = e.data.split(";")

            //Solicita informações do item para a tela de informações diversas
            sendInfoDiver();

    } catch (e) {
        Save Exception();
    }
}

function sendInfoDiver() {
    try {
        var returnToUrl = "FCADELEM.aspx";
        var msgAddr = window.location.protocol + "//" + window.location.host + returnToUrl;
        var o = parent.document.getElementById("ifr_FCADELEM");
        o.contentWindow.postMessage(elementsList, msgAddr);
    } catch (e) {
        Save Exception();
    }
}

Then, already in the modal screen that recem was opened, receives the object and starts the treatment.

window.addEventListener("message", receiveMessage, false);

function receiveMessage(e) {
    try {
        //url que verifica se post message é valido
        var url = window.location.protocol + "//" + window.location.host
        //testa se origem do post é do mesmo dominio
        if (e.origin !== url)
            return;

        if (typeof e.data == "object") {
            //Recebe o objeto item e popula campos do formulário
            setData(e.data);
        }

        //Oculta a mensagem de loading
        parent.hideLoading();

    } catch (e) {
       Save Exception();
    }
}

function setData(PlReturn) {
...
}

And to send the updated data back, just do the reverse path.

I appreciate the collaboration, and remains the source of research for future community doubts.

Hug

  • 1

    William, good you have answered in detail. Can you edit the question with the missing information? so it becomes clearer to those who seek solution to the same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.