PDA

View Full Version : Model class use Proxy Sample



softball
22 May 2014, 7:58 PM
Model class use Proxy Sample
Dear?
As the sample of Module useing Proxy?
In Console log
Q1.I can see "Saved Ed! His ID is 2 "
it seem to save the "Ed Spencer" to the fisrt recors of json file"users" ?
but I try to use userStore.getCount()?it still show 3 ? not 4 (if it has been added)?

Q2.


GET http://192.168.1.90/ext4/03/users3.json/2?_dc=1400817338870&id=2 404 (Not Found)


why?
I use Extjs.4.2.1

Thx




Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'age'],
proxy: {
type: 'rest',
url : 'users3.json',
reader: {
type: 'json',
root: 'users'
}
}
});

var userStore;
Ext.require('Ext.data.Store');
Ext.onReady(function() {
// Uses the User Model's Proxy
userStore = Ext.create('Ext.data.Store', {
model: 'User',
autoLoad: true
});

// Gives us a reference to the User class
var User = Ext.ModelMgr.getModel('User');

var ed = Ext.create('User', {
name: 'Ed Spencer',
age : 25
});

// We can save Ed directly without having to add him to a Store first because we
// configured a RestProxy this will automatically send a POST request to the url data/users
ed.save({
success: function(ed) {
console.log("Saved Ed! His ID is "+ ed.getId());
}
});

// Load User 1 and do something with it (performs a GET request to /users/1)
User.load(1, {
success: function(user) {
console.log("Loaded user 1: " + user.get('name'));
}
});

});

--------------users3.json is
{
success: true,
usersData: [
{id: 2, name: 'Tommy',age:40},
{id: 3, name: 'Ed',age:30},
{id: 4, name: 'Ed X',age:45}
]
}

scottmartin
24 May 2014, 7:33 AM
If you type in your URL in question 2 in a browser, do you get a result? what if you remove the _dc=_dc=1400817338870 from the URL

in your proxy:


proxy : {
type : 'ajax',
noCache : false, // remove param "_dc"
}


Is the record save to your table?, or just added to your store I see you are using a static JSON file, so I suspect you have just added it to your store and the result is based on your 3 static records in your JSON file.

softball
25 May 2014, 4:54 PM
if i type the url in browser as:


http://192.168.1.90/ext4/03/users3.json/2?_dc=1400817338870&id=2

is show 404
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

if i type the url in browser as:


http://192.168.1.90/ext4/03/users3.json/2?

i still show the same 404 message.

if i change
proxy: {
type: 'rest',
url : 'users3.json',
as ==>
proxy: {
type : 'ajax',
noCache : false, // remove param "_dc"
url : 'users3.json',

the it show ok as:
?Loaded user 1: Ed ?
no moore err message ,
Why???
why not rest ,and use ajax?


but, I still get userStore.getCount(); ==>3 ,not 4
because I am just start to learn Extjs ?
so i doen't know how to add it to db table,
I just learn form sample to try it.
So,the result may be as you said,is based on your 3 static records in your JSON file?

scottmartin
25 May 2014, 5:04 PM
You can still use rest, that was a cut/paste from code .. not important.

If you get a 404 from your URL, you need to find out why. Do you want the id? You can use 'appendId: false' in your proxy to remove that as well.

softball
25 May 2014, 6:06 PM
Dear scottmartin?
thank,
I had try use appendId: false, it work.
but,


UserMRef.load(3, {
success: function(userX) {
console.log("Loaded user 1: " + userX.get('name'));
}
});

no matter i change the number of load, it still load fist record.?
Is't will not load diffent record?

and ? I see the doc:
http://www.objis.com/formationextjs/lib/extjs-4.0.0/docs/api/Ext.data.proxy.Rest.html#load



user.save({
success:function(user){
user.set('name','Khan Noonien Singh');

user.save();//PUT /users/123
}
});

user.save call it's self again?

Does the "Load" not load the data by id ?
LOAD(3) -->load data from id=3
LOAD(5) -->load data from id=5
??

scottmartin
25 May 2014, 6:30 PM
You URL references a file: url : 'users3.json',

Are you using a database in your real app, as you cannot modify a JSON file directly. It will not change.

Scott.

softball
25 May 2014, 7:03 PM
Dear Scott?
I does not database, just use xx.json file to test.
So... it will not work with add/update/del motion? !=.=
and



Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'email'],

proxy: {
type: 'rest',
url : '/users'
}
});
the url:'/users' ,is not file?

by the way,

UserMRef.load(3, ...)

is't not load(number,...) , the lumber is id?
THX