1. #1
    Sencha User
    Join Date
    Jan 2012
    Posts
    15
    Vote Rating
    0
    nixerella is on a distinguished road

      0  

    Default Answered: grid, rest proxy in model, single item with extra processing

    Answered: grid, rest proxy in model, single item with extra processing


    I have a model with a rest proxy, a controller,a store and a grid. The grid is meant to display items, but there is one field (call it 'myfield') in an item that we only want to populate when the user double clicks that item.

    I am using Ext.ModelManager.getModel('mymodel') to get the model and then load the single record by calling my rest method that gets single items but my rest service cannot return the item exactly as I need - I need to do some processing on it. So my success function of modelInstance.load() does that processing and in there I do item.set('myfield',someProcessedValue). This seems to work ok in the debugger - I see that field get updated and extjs seems to set a dirty flag. The grid doesn't display this item so I don't see a dirty flag in the grid - that's what I want.

    However I have a button in my app, which when I click it, gets the grid's selected item by doing myGridName.getSelectionModel().getSelection() and getting the first item of that array. At that point the item's myfield is an empty string and not the someProcessedValue that I set it to.

    Can anyone please advise whether I am going about this in the correct way and/or what I am missing? I think I need to do something else after the item.set.. line.

  2. Admittedly I'm a bit confused by it all, but it seems to me that your model and store (which is what the grid display) are not in sync.

    Once you load your model from a proxy, the store should update with the records (by the way, I think it is generally better to have the proxy in the store rather the model - logically the model is just the footprint of the data the store holds).

    Anyway, I assume that when you use
    Ext.ModelManager.getModel('mymodel') to load a single record, you end up with a 'floating' record that is not synced to the grid. You'd need to manually feed the values of the record into the correct store record using set.
    Am I on track here or just adding to the confusion?

  3. #2
    Sencha User Izhaki's Avatar
    Join Date
    Apr 2009
    Location
    London
    Posts
    118
    Answers
    7
    Vote Rating
    13
    Izhaki will become famous soon enough

      0  

    Default


    How exactly was your model record added to the store?

  4. #3
    Sencha User
    Join Date
    Jan 2012
    Posts
    15
    Vote Rating
    0
    nixerella is on a distinguished road

      0  

    Default


    Initially the grid gets populated using the REST proxy that is defined in the model (it's calling a REST service method that gets all records). I didn't write any extra code to add each record to the store, extjs is doing it behind the scenes. At that point each record's myField is empty but the other fields are set.
    When the controller detects a double click of a grid item it is there that I do model.load() in order to call the REST method that gets the single item and that's when I need to do the processing and do the item.set(myField,processedValue). Possibly instead of doing model.load I should just do an ajax request to ...myRestService/items/1 or whatever item was clicked.

    In the success method of the model.load I am reading the value I want by doing response.raw.thevalue which seems wrong. I am wondering whether instead of returning an item from the REST service called thevalue I should just return an item called myField that contains the unprocessed value, process it and then the item.set might work. And also wondering whether I need to return a myField in the all records REST method.

  5. #4
    Sencha User Izhaki's Avatar
    Join Date
    Apr 2009
    Location
    London
    Posts
    118
    Answers
    7
    Vote Rating
    13
    Izhaki will become famous soon enough

      0  

    Default


    Admittedly I'm a bit confused by it all, but it seems to me that your model and store (which is what the grid display) are not in sync.

    Once you load your model from a proxy, the store should update with the records (by the way, I think it is generally better to have the proxy in the store rather the model - logically the model is just the footprint of the data the store holds).

    Anyway, I assume that when you use
    Ext.ModelManager.getModel('mymodel') to load a single record, you end up with a 'floating' record that is not synced to the grid. You'd need to manually feed the values of the record into the correct store record using set.
    Am I on track here or just adding to the confusion?

  6. #5
    Sencha User
    Join Date
    Jan 2012
    Posts
    15
    Vote Rating
    0
    nixerella is on a distinguished road

      0  

    Post


    I think you do understand the problem , despite my poor explanation. Thank you for persevering.

    I think I agree that the proxy ought to be in the store, but I moved it to the model because of a post I saw somewhere on here (don't have the link to hand) from someone who was trying to do what I'm doing i.e. call to a rest service to get a single item - the post specifically told them that they needed to move their proxy to the model.

    The difference for me is that what's returned by my service doesn't map straight onto what my store holds. I'm at home at the moment so can't post code but imagine I have a grid of Things and each Thing as defined in model/store has a name, a type, an id and a "geof" an array of geo features. I don't show the geof in the grid, just the other 3 things. My first REST method that gets all Things returns for each a name, type and id but no geo because it is too verbose. When I double click a grid item I want to get the geo for that record so I'm using the model instance's loadRecord to make the REST call to get that Thing from the database. In the REST method to get a single Thing I do get the geo, but I am returning it as "geo" not "geof" because there is some processing needed. So it looks a bit like this:

    modelInstance.loadRecord(id, this.doProcessing)
    ...
    doProcessing:function(myThing)
    {
    var something = myThing.raw.geo;
    //...do processing on something
    myThing.set("geof",processedSomething);
    }

    So I think you are saying that instead of the myThing.set("geof"... above which is just doing something to a disconnected record if I understand correctly, I need to get the record from the store by doing something like findRecord on the store and then do myStoreRecord.set("geof",processedSomething). Is that what you mean? It seems to make sense and I will try it.

    I'm also wondering whether I should not do the loadRecord and instead just do an ajax request to the REST url - I really don't like that myThing.raw.geo

  7. #6
    Sencha User Izhaki's Avatar
    Join Date
    Apr 2009
    Location
    London
    Posts
    118
    Answers
    7
    Vote Rating
    13
    Izhaki will become famous soon enough

      0  

    Default


    Quote Originally Posted by nixerella View Post
    modelInstance.loadRecord(id, this.doProcessing)
    ...
    doProcessing:function(myThing)
    {
    var something = myThing.raw.geo;
    //...do processing on something
    myThing.set("geof",processedSomething);
    }
    If console.log(myThing) after these lines show the right object? I believe it should.

    So I think you are saying that instead of the myThing.set("geof"... above which is just doing something to a disconnected record if I understand correctly, I need to get the record from the store by doing something like findRecord on the store and then do myStoreRecord.set("geof",processedSomething). Is that what you mean? It seems to make sense and I will try it.
    Yes that's what I was saying.

    I'm also wondering whether I should not do the loadRecord and instead just do an ajax request to the REST url - I really don't like that myThing.raw.geo
    loadRecord or REST is up to you really. Personally I prefer to struggle a bit with direct, MVC and autosync. But If I understand your problem correctly - I'm not sure REST would make any difference.

    As you said. Would really help to see some code!

  8. #7
    Sencha User
    Join Date
    Jan 2012
    Posts
    15
    Vote Rating
    0
    nixerella is on a distinguished road

      0  

    Default


    Thanks for your help - finding and updating the store record instead of the floating one was what I needed to do.

Thread Participants: 1