viezel / napp.alloy.adapter.restapi

RestAPI Sync Adapter for Titanium Alloy Framework
197 stars 102 forks source link

Fixed issue where model was being coverted into an array and assigned nu... #11

Closed TNuzzi closed 10 years ago

TNuzzi commented 11 years ago

...mbers as key rather than the key names.

Was having an issue where I was seeing the models being incorrectly created. I created a fix and did testing locally.

Here is the text output of the raw server response string and the output of the model.toJSON() after the fetch:

[DEBUG] : [REST API] server response [DEBUG] : { [DEBUG] : "v" = 0; [DEBUG] : "_id" = 51fdb7221cfa8e0000000002; [DEBUG] : domains = ( [DEBUG] : "ebay.com", [DEBUG] : "half.com", [DEBUG] : "paypal.com" [DEBUG] : ); [DEBUG] : isRegistered = 0; [DEBUG] : lastLocationsUpdate = "2013-08-04T02:06:26.987Z"; [DEBUG] : lastRoomsUpdate = "2013-08-04T02:06:26.987Z"; [DEBUG] : } [INFO] : model.toJSON() = {"_id":"51fdb7221cfa8e0000000002","v":0,"lastLocationsUpdate":"2013-08-04T02:06:26.987Z","lastRoomsUpdate":"2013-08-04T02:06:26.987Z","isRegistered":false,"domains":["ebay.com","half.com","paypal.com"]}

viezel commented 11 years ago

Sorry, I dont really see this going into the master branch. maybe im reading you PR wrong, but it seems like you are changing the method to your needs, instead of a general parsing. I might be wrong.

Can you create a test case, with a json feed. Then ill try it out.

imanc commented 11 years ago

I've the same problem. These JSON retrieved from server: {"codice":1269,"cliente":{"nome":"Gianluigi","cognome":"Barbieri","email":"","cf":"","via":"","cap":"27015","citta":"Landriano","provincia":"PAVIA (PV) - ","telefono":""},"data":"14-09-2013 00:06","stato":"Richiesta Fornitori","tracking":"","pagamento":"Bonifico","spedizione":"Corriere","speseSpedizione":"0,00","totOrdine":"60,00","totProdotti":"60,00","notePub":"","notePriv":"","prodotti":[{"codice":"BTe0042","nome":"Spaten Munchner Hell","formato":"50 cl.","disp":0,"qta":20,"prezzoUnitario":"3,00","totale":"60,00"}]}

and this is the model parsed (JSON.stringify):

{"0":1269,"1":{"nome":"Gianluigi","cognome":"Barbieri","email":"","cf":"","via":"","cap":"27015","citta":"Landriano","provincia":"PAVIA (PV) - ","telefono":"","id":"76dafaee-2ff1-ca60-0cfb-c089dd51e066"},"2":"14-09-2013 00:06","3":"Richiesta Fornitori","4":"","5":"Bonifico","6":"Corriere","7":"0,00","8":"60,00","9":"60,00","10":"","11":"","12":[{"codice":"BTe0042","nome":"Spaten Munchner Hell","formato":"50 cl.","disp":0,"qta":20,"prezzoUnitario":"3,00","totale":"60,00"}],"id":1269}

Note that instead of "codice" key I have the key "0", instead of "cliente" key I have the "1"....

Is this a bug or I did it wrong?

exports.definition = {  
    config: {
        "URL": "url obscured for privacy",
        //"debug": 1, 
        "adapter": {
            "type": "restapi",
            "collection_name": "ordine",
            "idAttribute": "id"
        },
    },      
    extendModel: function(Model) {      
        _.extend(Model.prototype, {});
        return Model;
    },  
    extendCollection: function(Collection) {        
        _.extend(Collection.prototype, {});
        return Collection;
    }       
};
var ordine = Alloy.createModel("ordine", {id: codice});
ordine.fetch({ 
    success : function(response){
         Ti.API.info(JSON.stringify(ordine));
    },
    error : function(){
        Ti.API.error("hmm - this is not good!");
    }
});

----SOLUTION---- On restapi.js line 204 I've edited from values.push( item ); to values[i] = item;

leoromanovsky commented 10 years ago

Hello - I've hit the same problem when fetching a single model. The parser in restapi will turn it into a list. What's a clean way around this without changing the sync adapter?

viezel commented 10 years ago

have a look at https://github.com/viezel/napp.alloy.adapter.restapi#nested-result-objects you can do your own custom parsing of your JSON feed.

leoromanovsky commented 10 years ago

Hi @viezel, so given an API endpoint like this:

http://someurl/api/cars/1

Which renders this JSON:

{
id: 1,
color: "Red",
image: "https://s3.amazonaws.com/red.jpg.jpg"
}

I think we want to use a Backbone model here, because it's a singular resource.

model.fetch()

extendModel: function(Model) {
        _.extend(Model.prototype, {
            parse: function (data) {
                Ti.API.info(data);
           }
        });

        return Model;
    },

The data passed into parse is not the raw JSON, but has already been turned into an array:

[INFO] :   (
[INFO] :       1,
[INFO] :       "Value1",
[INFO] :       "Value2",
[INFO] :       0
[INFO] :   )

Do you have any best practices to how to transform a dict-type response into a proper model?