1. #1
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default 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


  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,220
    Answers
    3521
    Vote Rating
    859
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Without having the store anywhere, can you load a single model instance by itself?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    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.

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,220
    Answers
    3521
    Vote Rating
    859
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    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
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  5. #5
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    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);
                    }

  6. #6
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    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?

  7. #7

  8. #8
    Sencha User
    Join Date
    Oct 2012
    Posts
    14
    Vote Rating
    0
    mlourdes is on a distinguished road

      0  

    Default 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 ??

Thread Participants: 2