-
30 Jan 2012 10:48 AM #1
Answered: How to get a single model
Answered: How to get a single model
I would like to input on how to best handle a situation. I have a store of users and a corresponding user model. The proxies for each are different - I assume Sencha will use the store proxy when I need a collection and the model proxy when I just need one?
In my app I sometimes need to get a user that may not be in my current store collection. So I want to download it directly and add it to the store. Unsure how to do this.
User model:
Code:Ext.define("App.model.User", { extend: "Ext.data.Model", config: { fields: ['id', 'name'], proxy: { type: 'ajax', url : config.url + "/user", reader: 'json' }, } });
Users store:
Code:Ext.define("App.store.Users", { extend: "Ext.data.Store", requires: "App.model.User", config: { autoLoad: true, model: "App.model.User", proxy: { type: 'ajax', url : config.url + "/users", reader: 'json' } } });
In my controller code - this doesn't work
Code:var d = Ext.data.StoreManager.lookup('Users'); var e = d.find('id', 'someid'); if(e == -1) { Ext.ModelManager.getModel('User').load('someid'); }
Errors are get:
Uncaught Error: [ERROR][Ext.data.Operation#setModel] An Operation needs to have a model defined. (if I try to call load on store instead of find)
Uncaught TypeError: Cannot call method 'load' of undefined (if I try to use modelmanager)
So how can I fetch a user by id from store and have the model automatically fetch it from proxy if not found?
Thanks
-
Best Answer Posted by cyberwombat
-
30 Jan 2012 11:52 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,085
- Vote Rating
- 453
- Answers
- 3153
Without having the store anywhere, can you load a single model instance by itself?
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.
-
30 Jan 2012 11:58 AM #3
Well I am unclear on how to load the model so no.
This var d = Ext.ModelManager.getModel('User') doesn't return anything.
I tried adding models: ['User'] at top of controller and/or in my Ext.application with no difference.
-
30 Jan 2012 12:05 PM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,085
- Vote Rating
- 453
- Answers
- 3153
You need to use the full class name... so if your model name is MyApp.model.User then you need to use getModel('MyApp.model.User'). Remember, the controllers, models, stores, and views config arrays will prepend text so if you use models : ['User'] then it will actually load MyApp.model.User
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.
-
30 Jan 2012 12:14 PM #5
Ah ok. But still get an error
Code:var d = Ext.data.ModelManager.getModel('SquareRoute.model.User'); var e = d.load(278); console.log(d);
Uncaught Error: [ERROR][Ext.data.Operation#setModel] An Operation needs to have a model defined.
My console also doesn't seem to show an object for d - not sure what it's returning
Code:function () { return this.constructor.apply(this, arguments); }
-
31 Jan 2012 1:06 PM #6
Ok.. been at this for hours and hours - could really use some help. How do I load a model? Everything I tried fails - whether I use datamanager/store or modelmanager/getmodel same issue:
Uncaught Error: [ERROR][Ext.data.Operation#setModel] An Operation needs to have a model defined.
My model is defined in my store, its loaded in my app, loaded in my controller, no go.
Only way I can see to make this work is to do a json call and create a new model by hand
Examples? suggestions?
-
31 Jan 2012 3:31 PM #7
-
13 Nov 2012 10:52 AM #8
record from model
record from model
I am new with sencha , I just solved my problem with your comment , I have the proxy configured in the model not in the store , and I wonder if bringing a single model at a time I would lost efficiency .
With the configuration I have (of the proxy in the model )How can I get all the records ??


Reply With Quote