Search code examples
extjsproxystore

ExtJS - Store proxy loading data using Array Reader


I am having problems loading an array of data from an external file into my store.

This is the file containing the data:

/data/contacts

[
        ["Lisa", "[email protected]", "555-222-3333"],
        ["Bart", "[email protected]", "555-222-3333"],
        ["Homer", "[email protected]", "555-222-3333"],
        ["Marge", "[email protected]", "555-222-3333"]
]

This is my store:

Ext.define('MyApp.store.Contacts', {
    extend: 'Ext.data.Store',
    autoLoad: true,
    alias: 'store.contacts',
    model: 'MyApp.model.Contact',

    proxy: {
        type: 'ajax',
        reader: {
            type: 'array'
        },
        url: '../data/contacts'
    }
});

This is my model:

Ext.define('MyApp.model.Contact', {
    extend: 'Ext.data.Model',
    alias: 'model.contact',

    fields: [
        {name: 'name', mapping: 0},
        {name: 'email', mapping: 1},
        {name: 'phone', mapping: 2},

    ]
});

And I am getting this error:

[E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: [
    ["Lisa", "[email protected]", "555-222-3333"],
    ["Bart", "[email protected]", "555-222-3333"],
    ["Homer", "[email protected]", "555-222-3333"],
    ["Marge", "[email protected]", "555-222-3333"]


]

Does anyone have any suggestions on what I am doing wrong, or what I should do? Preferably I do not want to change the format of the array in the data file.


Solution

  • Okay, I realized what I did wrong. It was a very stupid user error made by me.

    In my data file: /data/contacts, what I really had was this:

    [
        ["Lisa", "[email protected]", "555-222-3333"],
        ["Bart", "[email protected]", "555-222-3333"],
        ["Homer", "[email protected]", "555-222-3333"],
        ["Marge", "[email protected]", "555-222-3333"]
    
    ]
    
    
    // [
    //     {name: 'Lisa', email: '[email protected]', phone: '555-222-1212'},
    //     {name: 'Bart', email: '[email protected]', phone: '555-333-2212'},
    //     {name: 'Homer', email: '[email protected]', phone: '555-122-1212'},
    //     {name: 'Marge', email: '[email protected]', phone: '555-123-1212'}
    // ]
    

    I was previously testing other options for my JSON format, and I was silly enough to think that I could place comment //'s in a normal file.

    Removing the comments from the file will fix the error. The array defined in this file can now be loaded into the store.