1

Como puedo acceder a un array que contiene otro array que se llama name que esta en un IIFE (Immediately-invoked function expression) para que el usuario pueda ordenar la lista en javascript?

(function(window) {
    // variables
    var sortName = document.getElementById("sortName");
    var bills = null;
    var name = null;
    var type = null;
    var date = null;
    var amount = null;
    var addForm = document.getElementById("addForm");

    function composeBill(bill) {
        // create elements
        var containerNewBill = document.createElement('div');
        var nameValue = document.createElement('p');
        var typeValue = document.createElement('p');
        var dateValue = document.createElement('p');
        var amountValue = document.createElement('p');

        //New class for the new bill
        containerNewBill.className = "containerNewBill";
        containerNewBill.classList.add('bill');

        // name, type, date and amount content text
        nameValue.innerText = bill.name;
        typeValue.innerText = bill.type;
        dateValue.innerText = bill.date;
        amountValue.innerText = bill.amount;

        // append the sub-elements
        containerNewBill.appendChild(nameValue);
        containerNewBill.appendChild(typeValue);
        containerNewBill.appendChild(dateValue);
        containerNewBill.appendChild(amountValue);

        // save the element reference on the note object, so in the future a DOM query is not required to access the element
        bill.element = containerNewBill;

        return containerNewBill;
    }

    function addBill() {
        // object of the new bill
        var newBill = {
            name: addForm.name.value,
            type: addForm.type.value,
            date: addForm.date.value,
            amount: addForm.amount.value
        };

        // reset form inputs
        addForm.name.value = '';
        addForm.type.value = '';
        addForm.date.value = '';
        addForm.amount.value = '';

        composeBill(newBill);

        // add the new note to the DOM
        container.appendChild(newBill.element);

        // add the new note into the notes array
        bills.push(newBill);
    }

    function renderBills() {
        container = document.getElementById('billnote');
        var billnote = bills.map(composeBill);

        // append each note
        billnote.forEach(function(bill) {
            container.appendChild(bill);
        });

        // on submit note form event
        addForm.addEventListener('submit', function() {
            event.preventDefault();
            addBill();
        });
    }

    function main(data) {

        if (data && Array.isArray(data)) {
            bills = data;
            return renderBills();
        }

        return new Error('Bills Array params required', bills);
    }

    function saveNote() {
        // updated bill data
        name = addForm.name.value;
        type = addForm.type.value;
        date = addForm.date.value;
        amount = addForm.amount.value;

        // update note DOM for title and content
        // request the title and content elements based on the tag name
        // this request type returns a HTML collection inste of a single node
        var name = element.getElementsByTagName('p');
        var type = element.getElementsByTagName('p');
        var date = element.getElementsByTagName('p');
        var amount = element.getElementsByTagName('p');

        // validate the DOM queries
        if (name) name[0].innerText = name // use index 0 because is a HTML collection (array-like)
        if (amount) amount[0].innerText = amount; // use index 0 because is a HTML collection (array-like)
    }

    window.app = {
        main: main,
    };

}(window));

var data = [{
    name: addForm.name.value,
    type: addForm.type.value,
    date: addForm.date.value,
    amount: addForm.amount.value
}];

app.main(data);
1
  • Puedes agregar el contexto? Esos datos los sacas de un formulario? si quieres enviar data a main, por que no usas name: document.getElementById("addForm").name.value? es muy confuso lo que quieres hacer si no lo explicas bien :P el 5 mar. 2017 a las 4:48

1 respuesta 1

0

Siempre y cuando tengas acceso a la variable window o global en el scope actual:

(function()
{
  console.debug(window);
  //console.debug(global);
  window.a = "hello world";
  //global.a; = "hello world";
})();

setTimeout(function()
{
  console.debug(window.a);
  //console.debug(global.a);
},1000);

ES6

(() => {
  console.debug(window)
  //console.debug(global)
  window.a = "hello world";
 // global.a = "hello world";
})()

setTimeout(()=>
{
  console.debug(window.a);
  //console.debug(global.a);
},1000);

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.