Domanda

Recently I try to get data from json file with using sencha touch but its showing 0 result, Please refer below coding and tell me about my mistake,

Thanks,

app.js

Ext.application({
name: 'TP',

views: [
    'Main'
],

models: [
    'User'
],

stores: [
    'Users'
],

launch: function() {
    // Destroy the #appLoadingIndicator element
    Ext.fly('appLoadingIndicator').destroy();

    // Initialize the main view
    Ext.Viewport.add(Ext.create('TP.view.Main'));

    var user = Ext.create('TP.model.User', {
        name: 'James Henry',
        age: 24,
        phone: '555-555-5555',
        username: 'Admin'
    });

    Ext.getStore('Users').on('load', this.onStoreLoad, this);
    console.log(Ext.getStore('Users'));
},

onStoreLoad: function(self, records, success, operation){
    console.log(self);
}
});

Model :: User.js

Ext.define('TP.model.User',{
extend: 'Ext.data.Model',

config: {
    fields: [
        {name: 'name', type: 'string'},
        {name: 'age', type: 'int'},
        {name: 'phone', type: 'string'},
        {name: 'gender', type: 'string'},
        {name: 'username', type: 'string'},
        {name: 'alive', type: 'boolean', defaultValue: true}
    ],
    validations: [
        {type: 'presence', field: 'age'},
        {type: 'length', field: 'name', min: 2},
        {type: 'inclusion', field: 'gender', list: ['Male', 'Female']},
        {type: 'exclusion', field: 'username', list: ['Admin', 'Operator']},
        {type: 'format', field: 'username', matcher: /^[A-Za-z0-9 _]*$/ }
    ],
},

ageString: function(){
    var age = this.get('age');
    if(age > 1) {
        return age + " yesrs old";
    }else{
        return age = " Yes old";
    }
}
});

Store :: Users.js

Ext.define('TP.store.Users', {
extend: 'Ext.data.Store',

config: {
    model: 'TP.model.User',
    autoload: true,
    proxy: {
        type: 'ajax',
        url: 'http://localhost/sencha/SenchaStarter/data/users.json',
        reader: {
            rootProperty: 'users',
            type: 'json'
        }
    }
}
});

JSON file users.json

{
"users": [
    {
        "name": "Mike Henderson",
        "age": 24,
        "phone": "555-555-555",
        "gender": "Male",
        "username": "mhenderson",
        "alive": true
    },
    {
        "name": "Sally Michael",
        "age": 34,
        "phone": "555-555-555",
        "gender": "Female",
        "username": "sallym",
        "alive": true
    },
    {
        "name": "Rory Muldoon",
        "age": 19,
        "phone": "555-555-555",
        "gender": "Male",
        "username": "greatscott",
        "alive": true
    }
]
}
È stato utile?

Soluzione

The store isn't loading because you haven't loaded it yet. The reason is a typo as you wrote this config option in your store:

autoload

when it needs to be:

autoLoad

Altri suggerimenti

The proxy of your store is set to ajax. It won't save the records. I believe that you will only have access to the records in the onStoreLoad in app.js.

You could perhaps(now this is just a theory) set the ajax proxy in your model(this I am sure that you can do and is recommended by sencha because you can then override this proxy with the store proxy when needed http://www.sencha.com/learn/architecting-your-app-in-ext-js-4-part-2/) then save the records in the store, because i believe that a store proxy type by default is localstorage.

Good luck :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top