Search code examples
extjssencha-touch-2sencha-architect

List-View reading via JSON with no keys


I'm using Sencha Architect for testing purposes. I added a list view, connected with a store, connected with a model. The store is configured to load the following JSON data:

{
    "data": {
        "blacklist":[
            "word 1",
            "word 2",
            "word 2"
        ]
    }
}

Every "word x" should be listed as a list item in my list view

Now i wanted to set up my Model. But a model does need "field names", which in my case aren't present.

If i'm using the following JSON data:

{
    "data": {
        "blacklist":[
            {"name": "word 1"},
            {"name": "word 2"},
            {"name": "word 3"}
        ]
    }
}

and giving my model a field named "name", everything works fine.

But how do i set up this scenario, if my JSON values have no keys to define in the model (as field/fieldnames)?


Solution

  • Without knowing exactly what you want to do with your result you aught to be able to modify this code to get what you are looking for.

    App.js

    Ext.Loader.setConfig({
        enabled : true
    });
    
    
    Ext.application({
        name : ('SF' || 'SenchaFiddle'),
    
        models : ['TestModel'],
    
        stores : ['TestData'],
    
        launch : function() {
            Ext.create('Ext.dataview.List', {
                fullscreen : true,
    
                model : 'TestModel',
    
                store : 'TestData',
    
                itemTpl : "{blacklist}"
            });
        }
    });
    

    Model:

    Ext.define('SF.model.TestModel', {
        extend : 'Ext.data.Model',
    
        config : {
            fields : [{
                name : 'blacklist',
                type : 'string',
    
                // **This is where you will parse out the values of 'blacklist'**
    
                convert : function(value, record) {
                    console.log("value: ", value.length);
                    console.log("Record: ", record);
    
                    var returnArr = [];
                    if (value && value.length > 0) {
                        for (var i=0; i<value.length; i++) {
                            returnArr[i] = value[i];
                        }
    
                        return returnArr;
                    };
    
                    return "Could not Convert";
                }
            }]
        }
    });
    

    Store:

    Ext.define('SF.store.TestData', {
        extend : 'Ext.data.Store',
    
        config : {
            model : 'SF.model.TestModel',
    
            data : [{
                "blacklist":[
                    "word 1",
                    "word 2",
                    "word 2"
                ]
            }],
        }
    });
    

    Please let me know if I missed something in the question,

    Good luck, Brad