0

I've trying to get the sencha touch 2 data management examples to work but with no use. Here is the code of a simple model and store that are not working (getCount returns 0).

Ext.define('MyClient.model.Product', {
    extend:'Ext.data.Model',
    config:{
        fields:['name', 'image'],
        proxy:{
            type:'ajax',
            url:'http://localhost/st2/Projets/my-client-sencha/data/products.json',
            reader:{
                type:'json',
                rootProperty:'products',
                successProperty:'success'
            }
        }
    }
});

Ext.define('MyClient.store.ProductsStore', {
    extend:'Ext.data.Store',
    config:{
        model:'MyClient.model.Product',
        autoLoad:true,
        autoSync:true
    }
});

In the launch function I have these lines:

        var prod = Ext.create('MyClient.store.ProductsStore');
        prod.load();
        alert(prod.getCount());

And finally here's my products.json:

[

    {
        "name":"test"
    }
]

I'm not getting any errors in the console but still the getCount always returns 0. Can use some help here please.

EDIT: wrong JSON, not working with this neither:

{
    "success":true,
    "products": [

        {
            "name":"test"
        }
    ]
}
4

2 に答える 2

1

Because of your setting rootProperty:'products', your json has to be like

{
    products: [

        {
            "name":"test"
        }
    ]
}

if you do not want to change server response remover rootProperty from config.

have a look at Json Reader doc

Ahh... you forgot about asyn nature of the load()....

var prod = Ext.create('MyClient.store.ProductsStore');
prod.load(function ( ){
    alert(prod.getCount());
});

Notice that it prod.load() is using only for testing purposes, as far you have set property autoLoad: true.

In your snippet the loader would make 2 similar calls.

Cheers, Oleg

于 2012-08-30T14:14:38.897 に答える
0
Ext.define('MyBizilinkms.model.Customer', {
extend: 'Ext.data.Model',

config: {
    identifier:'uuid',
    fields: [
    'CustId',
    'EMail',
    'Title',
    'FName', 
    'MdInitial', 
    'LName', 
    'PhnNum', 
    'SecondPhnNo', 
    'DOB', 
    'Address', 
    'SecondAddress', 
    'City',
    'State', 
    'Zip', 
    'Country', 
    'RecieveEmail',
    'IsSaveonServer',
    { 
        name: 'Full_Name',
        type:'string',
        convert:function(v, rec) {
            return rec.data.FName + " " + rec.data.LName;
        }
    }],
    validations: [
    {
        type: 'format',   
        name: 'EMail', 
        matcher: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, 
        message:"Valid Email Required"
    },
    {
        name: 'PhnNum',             
        type : 'custom',
        message : "Valid Phone required",
        validator : function(config, value, model) {
            var reg = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
            return reg.test(value);
        }            
    },
    {
        name: 'SecondPhnNum',             
        type : 'custom',
        message : "Valid 2nd Phone required",
        validator : function(config, value, model) {
            if (!Ext.isEmpty(value)) {
                var reg = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
                return reg.test(value)
            }
            return true;
        }            
    },
    {
        type: 'presence', 
        name: 'FName', 
        message : "First Name is required"
    },
    {
        type: 'presence', 
        name: 'LName', 
        message : "Last Name is required"
    }, 
    {
        type: 'presence', 
        name: 'Address', 
        message : "Address is required"
    },
    {
        type: 'presence', 
        name: 'City', 
        message : "City is required"
    },
    {
        name: 'State',             
        type : 'custom',
        message : "Valid State required",
        validator : function(config, value, model) {
            var reg = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
            if(Ext.isEmpty(value))
                value = '00'
            var state = value.replace(/^\s+|\s+$/g, "");

            return reg.test(state)
        }            
    },
    {
        name: 'Zip',             
        type : 'custom',
        message : "Valid Zip required",
        validator : function(config, value, model) {
            var reg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
            return reg.test(value)
        }            
    },
    {
        type: 'presence', 
        name: 'Country', 
        message : "Country is required"
    }
    ]       
},
getFullName: function() {
    return this.get('FName') + '' + this.get( 'LName');
}
});
于 2013-06-05T20:21:12.997 に答える