Results 1 to 5 of 5

Thread: How to load a rest store with ID?

  1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    130
    Answers
    4

    Default How to load a rest store with ID?

    Here within a controller I create a rest store:

    Code:
    this.somePerson = Ext.create('Darkhorn.store.Human', {
        model: 'Darkhorn.model.Human',
        autoLoad: false
    });
    this.somePerson(123);
    But it doesn't load the person with id 123 even though I have set idProperty in the model. It loadds all the humans.

    This one loads can retrieve the human with ID 123.
    Code:
    Ext.ModelManager.getModel('Darkhorn.model.Human').load(record.data.ID, {
        success: function(recordd) {
            someHuman = recordd;
        }
    });
    It gets the human with ID 123 but then someHuman is not a store. Thus someHuman.sync() doesn't work, it says that sync() is not defined. Or if I use someHuman.save() it PUTs all the data to the server.

  2. #2
    Sencha Premium Member
    Join Date
    Jun 2011
    Posts
    46
    Answers
    5

    Default

    Quote Originally Posted by darkhorni View Post
    Here within a controller I create a rest store:

    Code:
    this.somePerson = Ext.create('Darkhorn.store.Human', {
        model: 'Darkhorn.model.Human',
        autoLoad: false
    });
    this.somePerson(123);
    But it doesn't load the person with id 123 even though I have set idProperty in the model. It loadds all the humans.

    This one loads can retrieve the human with ID 123.
    Code:
    Ext.ModelManager.getModel('Darkhorn.model.Human').load(record.data.ID, {
        success: function(recordd) {
            someHuman = recordd;
        }
    });
    It gets the human with ID 123 but then someHuman is not a store. Thus someHuman.sync() doesn't work, it says that sync() is not defined. Or if I use someHuman.save() it PUTs all the data to the server.
    Maybe I'm missing what you're trying to do, but I think you might be confused with the difference between a model and a store. A store is a collection of model records... or imagine it to be a glorified array of model instances.

    If you have the following:

    Code:
    var people = Ext.getStore('people'),
        person = MyApp.model.Person.create({ id: 1234 });
    If you're only wanting one instance of a person, MyApp.model.Person.load(1234) will give you what you're looking for and you can manipulate it however you want. Otherwise, what is the reason why you would want to get the people store and sync for just one person?

  3. #3
    Sencha Premium Member
    Join Date
    Jun 2011
    Posts
    46
    Answers
    5

    Default

    Keep in mind that the purpose of a store is to handle complex logic for every record that exists for that store. There might be a handful of records that need to be deleted, another handful that needs to be created, and many more that needs to be updated. store.sync() will will call all the necessary backend api's to handle the crud operations.

  4. #4
    Sencha User
    Join Date
    Aug 2011
    Posts
    130
    Answers
    4

    Default

    Quote Originally Posted by enagic View Post
    Maybe I'm missing what you're trying to do, but I think you might be confused with the difference between a model and a store. A store is a collection of model records... or imagine it to be a glorified array of model instances.

    If you have the following:

    Code:
    var people = Ext.getStore('people'),
        person = MyApp.model.Person.create({ id: 1234 });
    If you're only wanting one instance of a person, MyApp.model.Person.load(1234) will give you what you're looking for and you can manipulate it however you want. Otherwise, what is the reason why you would want to get the people store and sync for just one person?
    I have tried with:
    Code:
    person = MyApp.model.Person.create({ id: 1234 });
    but this has created an empty record. Actually I want to load it from my REST API.

    Then I have tried to use:
    Code:
    person = MyApp.model.Person.load(1234);
    console.log(person);
    it has GET (the browser has loaded the data) the data from the REST API but console.log says that person is not defined.

    One person has different properties such as e-mail and telephone. After I change his email I want to send this new data to server with PUT method aka person.save() or person.sync().

  5. #5
    Sencha Premium Member
    Join Date
    Jun 2011
    Posts
    46
    Answers
    5

    Default

    Ext.data.Model.create() will be exactly that... to create an instance. Whatever object literal you pass in will be the fields you want the model to be populated with. It'll be instantiated in memory only until you save() it.

    Ext.data.Model.load() will be asynchronous. You'll have to get the record with a callback.

    Once you get the desired record, you can edit it and Ext.data.Model.save(); once you're done.

    If you want to change the create/update method from POST to PUT, you'll have to change the ajax proxy configured to model to use PUT by changing Ext.data.proxy.Ajax.actionMethods, which is by default {create: 'POST', read: 'GET', update: 'POST', destroy: 'POST'}.

    [UPDATE]
    You can map the crud url operations with the Ext.data.proxy.Ajax.api configuration.

Similar Threads

  1. Ext.list with REST proxy type, load using model or store?
    By luisparada in forum Sencha Touch 2.x: Q&A
    Replies: 1
    Last Post: 29 Sep 2014, 4:25 AM
  2. Partial REST data load into store
    By madhu534 in forum Ext: Q&A
    Replies: 3
    Last Post: 19 Nov 2013, 2:17 PM
  3. Cannot load data into store via REST proxy
    By anonymous123456789 in forum Sencha Touch 1.x: Discussion
    Replies: 1
    Last Post: 17 Aug 2012, 12:40 PM
  4. Unable to load data from store with REST proxy into list
    By jaeger25 in forum Sencha Touch 2.x: Q&A
    Replies: 1
    Last Post: 3 Aug 2012, 3:46 PM
  5. load list store with rest service
    By marmara95 in forum Sencha GXT Q&A
    Replies: 1
    Last Post: 1 Jun 2012, 5:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •