0

Using JSON

Hey,

I am new to Widget Development. I am trying to create a view in which I want to display a list of contacts, each of them with its name, address, phone...

I have a file contacts.js

CODEBOX
{"contacts":[  
{"contact": { "id": "1", "name": "Joseph", "phone": "12345678" , "address": "C/ Marques de las Ensenada 5" }}
,
{"contact": { "id": "2", "name": "Mike", "phone": "23456789" , "address": "C/ Usera 12" }}
,
{"contact": { "id": "3", "name": "John", "phone": "98765432" , "address": "C/ Riverside 2" }}
]}


I have now in my contactListingView a variable called
CODEBOX
var contacList = eval ( "(" + contacts + ")" )


Sooo, resuming, I want now in my grid all the contacts that are included in the contacts object, and in the moment I press enter, I want to create a detail for each contact, having as label in the backbutton the name of the contact, but I have the problem of assigning the values to each of the cells, and to the backbutton.

Pleeeease, help me, is important!!!!

Thank you very much!!!!

Alvaro

7 Replies
  • Use of the eval function is prohibited. Please start with the Acceptance Criteria here:

    http://developer.yahoo.com/connectedtv/ass...ce_Criteria.doc

    and review the Quick Start guide here:

    http://developer.yahoo.com/connectedtv/quickstart/

    Thank you,
    Kelly
    0
  • QUOTE (kireland @ Apr 19 2010, 07:54 AM) <{POST_SNAPBACK}>
    Use of the eval function is prohibited. Please start with the Acceptance Criteria here:

    http://developer.yahoo.com/connectedtv/ass...ce_Criteria.doc

    and review the Quick Start guide here:

    http://developer.yahoo.com/connectedtv/quickstart/

    Thank you,
    Kelly



    It's true, I read the Acceptance Criteria when I downloaded everything, but I forgot it... sorry...
    I have had a look at the Starting Parametres, but I have some doubts...
    - I want to do sth similar to the thing is displayed in the widget gallery example... I have checked the source code there, and I have some doubts I cant understand. Here they are

    CODEBOX
    cellCreator: function () {
    return new $wg.views.WidgetCell({
    events: {
    onSelect: function (event) {
    KONtx.application.loadView('WidgetDetail', {
    widget: event.payload.dataItem.widget
    });
    }
    }
    });
    },


    can I just substitute the part of the widget by contact: event.payload.dataItem.contact... and if so, my logical question is of course... where in the code is that widget updated??? because I thought it was in the part of updateContent, where I have found the following

    CODEBOX
    var base     = this.config.baseId,
    title = dataItem.widget.title,
    author = dataItem.widget.authors[0].author,
    icon_src = $wg.API.gallery.getFileLink(dataItem.widget, 'icon'),
    version = dataItem.widget.version,
    install_state = dataItem.widget.install_state.status;

    // log(dataItem.widget.wid, install_state);

    this.widget_name.setText(title);
    this.author_name.setText($_(base+'.author_name', author.name));

    this.widget_icon.setSource(icon_src);
    this.install_badge.setSource($wg.assets[base+'.install_badge.'+install_state]);


    but I havent found anything in the code in reference to dataItem.widget.authors[0].author (and the rest of the cases)... where is that array??? Because it seems there is a database, or something like that, but I havent found it!!!!!!

    Resuming... I have my listing configured as shows

    1. some arrays at the beginning as variables called "names []", "dirs []", "phones []"
    2. the initialize function as shown

    CODEBOX
    initialize: function() {
    this.parent();
    this.cache.imgData = [
    "http://farm4.static.flickr.com/3464/3237417064_50c0815e3b_t.jpg",
    "http://farm4.static.flickr.com/3401/3237554258_0aa9b7e0e5_t.jpg",
    "http://farm4.static.flickr.com/3263/3236466402_166b392551_t.jpg",
    ].slice(0,3).map(function(src,i) {
    return {src: src,
    name: names[i],
    label:'Contact: '+ names[i],
    phone: phones[i],
    dir: dirs[i],
    }
    });
    }, /*



    3. I have the CellCreator in the following way...

    CODEBOX
    var cell = new KONtx.control.GridCell({
    events: {
    onSelect: function (event){
    // var dataItem = this.getDataItem();
    // log('selected a cell!',dataItem);
    KONtx.application.loadView('DetailContactsView', {
    contact: event.payload.dataItem.contact
    });
    }
    }
    });


    4. I have the UpdatingCell in the following

    CODEBOX
    imageCellUpdater: function (cell, dataitem) {
    cell.pic.src=dataitem.src;
    cell.name.data="Contact: " + dataitem.name;
    cell.dir.data="Cocina: " + dataitem.dir;
    cell.phone.data="Precio: " + dataitem.phone;
    },


    (the cell.pic, cell.name and so on have been created with its style...)
    HOWEVER, I DONT KNOW HOW TO ADAPT THE VIEW UPLOADED ONCE PRESSING "ENTER" IN THE LIST IN A WAY I CAN USE THIS PARAMETERS IN THAT VIEW!!!!!

    I really beg you to help me in this task, is kind of urgent, and I really dont know how to do it...
    0
  • QUOTE (Alvaro @ Apr 19 2010, 08:47 AM) <{POST_SNAPBACK}>
    HOWEVER, I DONT KNOW HOW TO ADAPT THE VIEW UPLOADED ONCE PRESSING "ENTER" IN THE LIST IN A WAY I CAN USE THIS PARAMETERS IN THAT VIEW!!!!!

    I really beg you to help me in this task, is kind of urgent, and I really dont know how to do it...

    Hi Alvaro,
    I'm not sure that I'm following. Are you wondering how to access the data in a cell after you've selected it and gone to your detail view? If that's the case, you need to pass the information you wish to pass to the next view in the object literal that's the second parameter of the KONtx.application.loadView() method. You can then access this information in the this.persist object of the view that's loaded.

    Take a look here:
    http://developer.yahoo.net/forum/index.php?showtopic=5307

    - Ben
    0
  • QUOTE (Benjamin Toll @ Apr 19 2010, 07:04 PM) <{POST_SNAPBACK}>
    Hi Alvaro,
    I'm not sure that I'm following. Are you wondering how to access the data in a cell after you've selected it and gone to your detail view? If that's the case, you need to pass the information you wish to pass to the next view in the object literal that's the second parameter of the KONtx.application.loadView() method. You can then access this information in the this.persist object of the view that's loaded.

    Take a look here:
    http://developer.yahoo.net/forum/index.php?showtopic=5307

    - Ben


    thanks, Ben, that was one of the parts I wanted to know!!!!!

    the second would be how to use an object JSON I have created in another file, from a new view file... I have read there is no possibility of using the eval function, so I will have to choose other way...
    0
  • QUOTE (Alvaro @ Apr 20 2010, 04:53 AM) <{POST_SNAPBACK}>
    thanks, Ben, that was one of the parts I wanted to know!!!!!

    the second would be how to use an object JSON I have created in another file, from a new view file... I have read there is no possibility of using the eval function, so I will have to choose other way...

    Use JSON.parse() instead of eval.

    - Ben
    0
  • QUOTE (Benjamin Toll @ Apr 20 2010, 11:05 AM) <{POST_SNAPBACK}>
    Use JSON.parse() instead of eval.

    - Ben


    Thank you Ben... but I have another problem

    Im trying to use the JSON.parse function, but it's not working... I think is related with my file structure.
    I have my JSON object description in contactsList.js, in the same folder of the view files, in a variable called list_of_contacts (I want to be this JSON object like a global variable)

    CODEBOX
    var list_of_contacts = {"contacts":[  
    {"contact": { "@id": "0", "name": "John" }}
    ,
    {"contact": { "@id": "1", "name": "George" }}
    ,
    {"contact": { "@id": "2", "name": "Luke" }}
    ]}



    And in the view file, called tableOfContacts.js, I have done

    CODEBOX
    var cont = JSON.parse(list_of_contacts);


    and finally I want to access the data of that variable by

    CODEBOX
    var name = cont.contacts[0].contact["name"];


    Any idea???
    0
  • When you are typing in the literal Javascript, it's not JSON and thus doesn't need JSON.parse(). You use JSON.parse when you have a string of JSON (like from an XHR fetch).
    0

Recent Posts

in Getting Started / Beginners - Yahoo! TV Widgets