Lorenz Meyer
Lorenz Meyer

Reputation: 19945

How to load json data from ajax in ExtJs

I try to get started with ExtJs, and I have quite basic question.
I have a model

Ext.define('Mb.model.User', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' },
        ...
    ],
    proxy: {
        type: 'ajax',
        url : 'server/gui/getUser.php'
    }
});

getUser.php returns a json string (it is the logged in user, and not a user out of a user table):

{"id":"1","name": ... }

I tried the following to load the data, but I get an error Uncaught TypeError: Object [object Object] has no method 'load'

Ext.define('Mb.Application', {
    ...
    launch: function() {
        ....
        user = Ext.create('Mb.model.User');
        user.load();
    }
});

What is the correct way to load that data ?


An additional question: What is the benefit of using Modelhere ?

Couldn't I do something like this ?

Ext.Ajax.request({
    url: 'server/gui/getUser.php',
    success: function(response){
    var user = Ext.JSON.decode(response.responseText);
    }
});  

Upvotes: 2

Views: 7164

Answers (2)

VISHNU
VISHNU

Reputation: 11

You can do, but with little changes

myRequest = Ext.Ajax.request({
  url: 'getdata.php',
  method: 'GET',
  callback: function(response) {
    console.log(response.responseText);
  }
});

Upvotes: 1

Evan Trimboli
Evan Trimboli

Reputation: 30092

In this case load is a static method. You can load a model from your server by passing the id.

Mb.model.User.load(id, {
    success: function(rec) {
        console.log('loaded', rec.getData());
    }
});

The advantage to using a model is the layer of abstraction, + the extra functionality you get from using a model.

Upvotes: 1

Related Questions