1. #1
    Sencha User
    Join Date
    Sep 2012
    Posts
    27
    Vote Rating
    0
    sveatlo is on a distinguished road

      0  

    Default Answered: extjs treestore with proxy

    Answered: extjs treestore with proxy


    I'm creating a MVC extjs application. I've got a treepanel with a store, which is loading the data from a php source. I get the following json-formatted response:

    Code:
    {
        "success": true,
        "root": [
            {
                "text": "Home",
                "leaf": true,
                "dbName": "NULL",
                "children": []
            },
            {
                "text": "Moje Firma s.r.o.",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo"
                    }
                ]
            },
            {
                "text": "Já Živnostník",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_de"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_de"
                    }
                ]
            },
            {
                "text": "Nezisková organizace",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_neziskova"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_neziskova"
                    }
                ]
            },
            {
                "text": "Příspěvková organizace",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_prispevkovka"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_prispevkovka"
                    }
                ]
            },
            {
                "text": "Moje Firma SK s.r.o.",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_sk"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_sk"
                    }
                ]
            }
        ]
    }

    My store:


    Code:
    Ext.define('Statistics.store.Menu', {    extend: 'Ext.data.TreeStore',
        model: 'Menu',
        autoLoad: true,
        autoSync: true,
        
        proxy   : {
            type : 'ajax',
            url  : 'resources/scripts/get_menu.php',
            reader: {
                type: 'json',
                root: 'root'
            }
        }
        
        
    });
    And my models:
    Code:
    Ext.define('Statistics.model.Menu', {    extend: 'Ext.data.Model',
        
        fields: [
            {name: 'text', type: 'string'},
            {name: 'leaf', type: 'boolean'},
            {name: 'expanded', type: 'boolean', defaultValue: false} 
        ],
        
        hasMany: {model: 'MenuItem', name: 'children'}
    
    
    });
    Code:
    Ext.define('Statistics.model.MenuItem', {
        extend: 'Ext.data.Model',
        
        fields: [
            {name: 'text', type: 'string'},
            {name: 'leaf', type: 'boolean'},
            {name: 'dbName', type: 'string'}, 
        ],
        
        belongsTo: 'Menu'
    
    
    });

    This configuration works, when the data are saved in a .json file. But id doesn't work, when they are loaded from a php source.

    Thanks for any answer.

  2. the code is on another computer, i'll post it later!

    but from what i see here, you should get rid of this:
    Code:
    <meta charset="utf-8" />

  3. #2
    Sencha User
    Join Date
    Feb 2011
    Posts
    6
    Vote Rating
    0
    nferreira is on a distinguished road

      0  

    Default


    What is the error you receive? Did you check javascript console for errors?

  4. #3
    Sencha User
    Join Date
    Sep 2012
    Posts
    27
    Vote Rating
    0
    sveatlo is on a distinguished road

      0  

    Default


    thx for reply.
    I'm getting following error in chrome console on line 42320(ext-all-debug-w-comments):
    Code:
    Uncaught TypeError: Cannot read property 'id' of undefined

  5. #4
    Sencha User chramer's Avatar
    Join Date
    Feb 2011
    Location
    Chisinau, Moldova
    Posts
    203
    Answers
    42
    Vote Rating
    10
    chramer will become famous soon enough

      0  

    Default


    When you are setting associations, each model must have an "id" property...
    Also : "The owner model is expected to have a foreign key which references the primary key of the associated model:"
    Read this : foreignKey
    Anyway I think you are complicating your life here with associations..
    Your treestore can be easily handled with a single model!

  6. #5
    Sencha User
    Join Date
    Sep 2012
    Posts
    27
    Vote Rating
    0
    sveatlo is on a distinguished road

      0  

    Default


    thx for reply.
    I've changed the Menu model to:
    Code:
    Ext.define('Statistics.model.Menu', {
        extend: 'Ext.data.Model',
        
        fields: [
            {name: 'text', type: 'string'},
            {name: 'leaf', type: 'boolean'},
            {name: 'expanded', type: 'boolean', defaultValue: false},
            {name: 'dbName', type: 'string', defaultValue: 'NULL'}
        ],
    
    
    });
    And I've deleted the MenuItem model,but it still doesn't work, when loading from .php file, it's working fine, from .json file

  7. #6
    Sencha User chramer's Avatar
    Join Date
    Feb 2011
    Location
    Chisinau, Moldova
    Posts
    203
    Answers
    42
    Vote Rating
    10
    chramer will become famous soon enough

      0  

    Default


    Can you see the response from the server when you are loading the store from a php script ?
    I tested your code and it works fine for me, both php and json..

  8. #7
    Sencha User
    Join Date
    Sep 2012
    Posts
    27
    Vote Rating
    0
    sveatlo is on a distinguished road

      0  

    Default


    This is the whole response:
    Code:
    <meta charset="utf-8" />
    {
        "success": true,
        "root": [
            {
                "text": "Home",
                "leaf": true,
                "dbName": "NULL",
                "children": []
            },
            {
                "text": "Moje Firma s.r.o.",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo"
                    }
                ]
            },
            {
                "text": "Já Živnostník",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_de"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_de"
                    }
                ]
            },
            {
                "text": "Nezisková organizace",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_neziskova"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_neziskova"
                    }
                ]
            },
            {
                "text": "Příspěvková organizace",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_prispevkovka"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_prispevkovka"
                    }
                ]
            },
            {
                "text": "Moje Firma SK s.r.o.",
                "leaf": false,
                "expanded": false,
                "children": [
                    {
                        "text": "Vydane",
                        "leaf": true,
                        "dbName": "demo_sk"
                    },
                    {
                        "text": "Prijate",
                        "leaf": true,
                        "dbName": "demo_sk"
                    }
                ]
            }
        ]
    }
    And my php script:
    Code:
    <meta charset="utf-8" />
    <?php
    require_once 'HTTP/Request2.php';
    $request = new HTTP_Request2('http://demo.flexibee.eu/c.json', HTTP_Request2::METHOD_GET);
    $request->setAuth('winstrom', 'winstrom', HTTP_Request2::AUTH_DIGEST);
    try {
        $response = $request->send();
        if (200 == $response->getStatus()) {
            $json = $response->getBody();
        } else {
            echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                 $response->getReasonPhrase();
        }
    } catch (HTTP_Request2_Exception $e) {
        echo 'Error: ' . $e->getMessage();
    }
    
    
    
    
    $obj = json_decode($json);
    $data = $obj->{'companies'}->{'company'};
    
    
    $i = 1;
    echo '{"success": true,"root": [{"text": "Home","leaf": true},';
    foreach ($data as $item) {
      if ( $i != count($data) ) {
        echo '{
    			"text"     : "'.$item->{'nazev'}.'",
    			"leaf"	   : false,
    			"expanded" : false,
    			"children" : [
    				{
    					"text" : "Vydane",
    					"leaf" : true,
    					"dbName"   : "'.$item->{'dbNazev'}.'"
    				},
    				{
    					"text" : "Prijate",
    					"leaf" : true,
    					"dbName"   : "'.$item->{'dbNazev'}.'"
    				}
    			]
    		},';
      } else {
        echo '{
    			"text"     : "'.$item->{'nazev'}.'",
    			"leaf"	   : false,
    			"expanded" : false,
    			"children" : [
    				{
    					"text" : "Vydane",
    					"leaf" : true,
    					"dbName"   : "'.$item->{'dbNazev'}.'"
    				},
    				{
    					"text" : "Prijate",
    					"leaf" : true,
    					"dbName"   : "'.$item->{'dbNazev'}.'"
    				}
    			]
    		}';
      }
      $i++;
    }
    echo ']}'
    ?>

  9. #8
    Sencha User
    Join Date
    Sep 2012
    Posts
    27
    Vote Rating
    0
    sveatlo is on a distinguished road

      0  

    Default


    Quote Originally Posted by chramer View Post
    Can you see the response from the server when you are loading the store from a php script ?
    I tested your code and it works fine for me, both php and json..
    Can you post the whole code, you tested it on? Just to make sure, I'm not doing something crazy.

  10. #9
    Sencha User chramer's Avatar
    Join Date
    Feb 2011
    Location
    Chisinau, Moldova
    Posts
    203
    Answers
    42
    Vote Rating
    10
    chramer will become famous soon enough

      0  

    Default


    the code is on another computer, i'll post it later!

    but from what i see here, you should get rid of this:
    Code:
    <meta charset="utf-8" />

  11. #10
    Sencha User
    Join Date
    Sep 2012
    Posts
    27
    Vote Rating
    0
    sveatlo is on a distinguished road

      0  

    Default


    IT WORKS!!!
    Thank you for help!

Thread Participants: 2