I’m trying to build a simple user setting feature for my school project.
I’ve created a data model like so:
Ext.define('Urlopy.Model.UserPreferences', {
extend : 'Ext.data.Model',
idProperty : 'userID',
fields : [{
name : 'userID',
type : 'string'
}, {
name : 'admin',
type : 'bool'
}]
});
and a store like this:
Ext.define('Urlopy.Store.UserPreferences', {
extend : "Ext.data.Store",
autoLoad : false,
autoSync : true,
batch : false,
proxy : {
type : 'ajax',
sortParam : undefined,
startParam : undefined,
pageParam : undefined,
limitParam : undefined,
noCache : false,
headers : {
"Content-Type" : "application/json; charset=utf-8"
},
api : {
read : 'services/UserPreferences.asmx/Get',
update : 'services/UserPreferences.asmx/Update'
},
reader : {
type : 'json',
root : 'd.data'
},
writer : {
type : 'json',
encode : false,
writeAllFields : true,
root : 'data'
}
},
model : 'Urlopy.Model.UserPreferences'
});
In my application when user logs in I would like to get his preferences.
I have a function like so:
onLogin : function() {
this.createGlobalStores();
this.userPreferenceStore.load({
callback : function(r, options, success) {
console.log(r.data)
}
});
},
createGlobalStores : function() {
this.userPreferenceStore = Ext.create("Urlopy.Store.UserPreferences");
},
What I would like to do in callback is to get userID and display it, even using simple alert.
Right now all I get is undefined that shows in firebug.
My json data from server looks like this:
{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}
I need to be able to call load method on that store and in callback get specific property from model. My store will always have 1 item! (is there another way to do this?)
Any ideas on why this isn’t working are welcome!
EDIT
I’ve changed my server script to return json like this:
{"d":{"data":{"userID":"12-44-55","admin":true}}}
This works fine, there were no need to add extra [] around object.
First of all a load for a store expects an array, and second you set the root for the proxy wrong. since there is no d.data object in your json. Simple fix will be to encode your json data as an array with one element, and the tag for that array to be “data” …
and just set the root to d
After this on the callback you’ll have an array of records with one element that you can simply access with index 0
EDIT
Other method not involving stores
Do a simple ajax request like:
I don’t know what you do with the data, but for example you can load it into a form, and for the update method then you will have the submit option for the form. which can be configured with the url where to send the data.