-
19 Jul 2012 8:27 AM #1
Answered: Basic model -> store -> fieldset setup question
Answered: Basic model -> store -> fieldset setup question
I've searched the documentation for hours now and can't find an example of a Store with a Model and data that hooks up to a fieldset xtype to display the Store's data in textfield and textareafield xtypes in the fieldset.
Here's my example setup
Model:
Store:Code:Ext.define('GS.model.HomeItem', { extend: 'Ext.data.Model', config: { fields: ['results'] } });
View:Code:Ext.define('GS.store.Home', { extend : 'Ext.data.Store', config : { model : 'GS.model.HomeItem', proxy : { type : 'jsonp', url : 'https://localhost:8443/...', /*returns: {"results":[[301.27]]}*/ reader : { type : 'json', rootProperty : '' } }, autoLoad : true } });
This currently displays the field with no value. How do I fix this? Does someone have a good example since "Using Forms" and "Using Stores" doesn't cover hooking up the two together?Code:Ext.define('GS.view.Home', { extend : 'Ext.Panel', xtype : 'homepanel', scrollable : true, config : { title : 'Home', styleHtmlContent : true, items : [ { xtype : 'panel', html : '<h2>Welcome</h2>' }, { xtype : 'fieldset', title : 'Today\'s Summary', store : 'Home', items : [ { xtype : 'textfield', label : 'Sales', name : 'results' } ...
-
Best Answer Posted by andy370
Apparently when I log the store object to the console it somehow updates the console when my asynchronous reply comes back. So I can't rely on looking at my store object in the console to know that data should be there.
- I implemented the solution found here http://www.sencha.com/forum/showthre...a-from-a-store by adding an event handler for the loading of the data.
- Removed "autoload: true" from the store (I only need the data when the view loads, so I'll handle it here in the init of my view)
- Fixed my Json response to be better formatted. ( {"results":301.27} instead of {"results":[[301.27]]} )
Code:Ext.define('GS.store.Home', { extend : 'Ext.data.Store', config : { model : 'GS.model.HomeItem', proxy : { type : 'jsonp', url : 'https://localhost:8443/...', /*returns: {"results":301.27}*/ reader : { type : 'json', rootProperty : '' } } } });Hopefully posting this helps someone else.Code:loadInitHome: function() { var homeViewPanel = this.getHome(); var homeStore = Ext.getStore('homeStore'); homeStore.load(); homeStore.on('load', function(homeStore, records, successful) { homeViewPanel.down('formpanel').setRecord(homeStore.getAt(0)); }, this); },
-
21 Jul 2012 12:14 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
- Answers
- 3108
If you had a form you could use the setRecord method on the form to load the model data into the fields
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
23 Jul 2012 1:59 PM #3
That got me started down the right path and I was able to assign a hard-coded value like this:
Now I'm trying to get my data from the store I mentioned earlier like this:Code:var homeViewPanel = this.getHome(); var newHomeItem = Ext.create("GS.model.HomeItem", { results: '101.20' }); homeViewPanel.down('formpanel').setRecord(newHomeItem);
But it doesn't load anything on the form. I check the homeStore in the console and it does contain a value for my results field. Could it have to do with how my data is being returned to the store?Code:var homeViewPanel = this.getHome(); var homeStore = Ext.getStore('homeStore'); homeViewPanel.down('formpanel').setRecord(homeStore);
Code:Ext.data.JsonP.callback1({"results":[[123.45]]});
-
24 Jul 2012 8:27 AM #4
Another clue
Another clue
When I view my store loaded from JsonP:
And view it in my console, I can expand it:Code:console.log(homeStore);
> Ext.apply.create.Class
> > data: Ext.apply.create.Class
> > > all: Array[1]
> > > > 0: Ext.apply.create.Class
> > > > > data: Object
> > > > > > id: "ext-record-22"
> > > > > > results: Array[1]
> > > > > > > 0: Array[1]
> > > > > > > > 0: 301.27
> > > > > id: "ext-record-22"
> > > > length: 1
And I would expect to be able to get at least that length of 1 at the end with this:
But that only returns "0";Code:console.log(homeStore.data.all.length);
I was also hoping to get the first (and only) item in the store using this:
But that only returns "undefined"Code:console.log(homeStore.getAt(0));
How come I can view things in my store from the console by expanding down to it, but can't access them via script?
-
24 Jul 2012 1:14 PM #5
Figured it out
Figured it out
Apparently when I log the store object to the console it somehow updates the console when my asynchronous reply comes back. So I can't rely on looking at my store object in the console to know that data should be there.
- I implemented the solution found here http://www.sencha.com/forum/showthre...a-from-a-store by adding an event handler for the loading of the data.
- Removed "autoload: true" from the store (I only need the data when the view loads, so I'll handle it here in the init of my view)
- Fixed my Json response to be better formatted. ( {"results":301.27} instead of {"results":[[301.27]]} )
Code:Ext.define('GS.store.Home', { extend : 'Ext.data.Store', config : { model : 'GS.model.HomeItem', proxy : { type : 'jsonp', url : 'https://localhost:8443/...', /*returns: {"results":301.27}*/ reader : { type : 'json', rootProperty : '' } } } });Hopefully posting this helps someone else.Code:loadInitHome: function() { var homeViewPanel = this.getHome(); var homeStore = Ext.getStore('homeStore'); homeStore.load(); homeStore.on('load', function(homeStore, records, successful) { homeViewPanel.down('formpanel').setRecord(homeStore.getAt(0)); }, this); },


Reply With Quote