Results 1 to 4 of 4

Thread: Load data to the store via httpProxy

  1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    6

    Default Answered: Load data to the store via httpProxy

    Hello! I have MySQL test database with movies and want to load data from there and display them into the combobox
    I've created a store and reader like this
    Code:
      var genres = new Ext.data.Store({
                reader:new Ext.data.JsonReader({
                    fields:['id','genre_name'],
                    root:'rows'
                }),
            proxy: new Ext.data.HttpProxy({
            url: 'genres.php'
            })
           });
    sufficient part of 'genres.php' page is looks like this
    PHP Code:
      $query="SELECT * FROM genres";
      
    $result=mysql_query($query);
           while(
    $obj mysql_fetch_object($result)) {
               
    $arr[] = $obj;
           }
           Echo 
    '{rows:'.json_encode($arr).'}'
    If I run this page in Firefox, I get the following output
    Code:
    {rows:[{"id":"1","genre_name":"Comedy"},{"id":"2","genre_name":"Drama"},{"id":"3","genre_name":"Action"},{"id":"4","genre_name":"Mystery"}]}
    now I'm trying to load data into the store
    Code:
    genres.load();
    And I think I have error right here. I 've set a break point at the last line of code and when FireBug stops on it, I see, that there is no lines in the genres. Just to be safe I tried to use genres store in the combobox
    Code:
    {
        xtype: 'combo',
        hiddenName:'genre',
         fieldLabel:'Genre',
         mode:'local',
         store:genres1,
         displayField:'genre_name',
          valueField:'id',
          width:200
    }
    But when I clicked combobox, all what I got is the spinning wheel and 'Loading' text.
    I don't understand how
    Code:
    genres.load()
    works. Is it goes to the 'genres.php' script and retrieves data from it's Echo call? And what's happening next? I am a bit newby in this techonology, so please, sorry for a maybe dumb questions.

  2. try changing your model and store to this style:

    Code:
    Ext.define("User", {
         extend: 'Ext.data.Model',
         fields: [
             'id', 'name'
         ]
     });
    
    Ext.create('Ext.data.Store', {
         model: 'User',
         proxy: {
             type: 'ajax',
             url : 'users.json',
             reader: {
                 type: 'json',
                 root: 'users'
             }
         }
     });
    Note that the reader is part of the proxy here and you just define the type using the type config in the reader.

    Next. see the console of Firebug in Firefox. It shows you the requests and responses. you can see if the store actually sent a request and what the response was. You may also try the following to see the records in the store:

    Code:
    genres.on('load', function() {
      console.log(genres.data)
    });

  3. #2
    Touch Premium Member
    Join Date
    Jun 2011
    Posts
    1,102
    Answers
    113

    Default

    try changing your model and store to this style:

    Code:
    Ext.define("User", {
         extend: 'Ext.data.Model',
         fields: [
             'id', 'name'
         ]
     });
    
    Ext.create('Ext.data.Store', {
         model: 'User',
         proxy: {
             type: 'ajax',
             url : 'users.json',
             reader: {
                 type: 'json',
                 root: 'users'
             }
         }
     });
    Note that the reader is part of the proxy here and you just define the type using the type config in the reader.

    Next. see the console of Firebug in Firefox. It shows you the requests and responses. you can see if the store actually sent a request and what the response was. You may also try the following to see the records in the store:

    Code:
    genres.on('load', function() {
      console.log(genres.data)
    });

  4. #3
    Sencha User
    Join Date
    Mar 2012
    Posts
    6

    Default

    Farish, thank you a lot, your suggestion to change model and store hellped me to solve the problem! Thank you!

  5. #4
    Touch Premium Member
    Join Date
    Jun 2011
    Posts
    1,102
    Answers
    113

    Default

    glad i could help. you dont necessarily need to define a model separately. you may use the fields config in the store directly (but not in the reader as you had done in the original post).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •