viezel / napp.alloy.adapter.restapi

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

Having trouble processing RESTapi response. #6

Closed kevbaker closed 11 years ago

kevbaker commented 11 years ago

The issue

I am using RESTapi to interface with my Rails-based REST API. I am having issues parsing the response.

It GETs my data from the API fine. I can see the response JSON, but I am unable to pull the individual models out of the responded collection for processing. I'm sure I'm doing some thing obviously wrong.

BTW: I have validated the JSON response through both curl and jsonlint.

Code:

models/Job.js


exports.definition = {
    config: {
        URL: "http://192.168.1.137:5000/jobs",
        columns: {
            "id": "string",
            "job_id": "INTEGER PRIMARY KEY AUTOINCREMENT",
            "company_name": "string",
            "address1": "string",
            "address2": "string",
            "city": "string",
            "state": "string",
            "postal_code": "string",
            "country": "string",
            "account_id": "string"
        },
        adapter: {
            "type": "restapi",  // sql || restapi
            "collection_name": "Job",
            "idAttribute": "job_id"
        }
    },
    extendModel: function(Model) {      
        _.extend(Model.prototype, {
          /*
           * setAdapter: this allow for runtime swithcing
           * of sync adapter. It is mostly used to switch
           * between local sqlite and remote REST adapters
           */
          parse : function(_resp, xhr) { debugger;
            // return the post attributes
            return _resp;
          },
          doTest : function(msg){
            alert("job model do Test:"+msg);
          }

        });

        return Model;
    },
    extendCollection: function(Collection) {        
        _.extend(Collection.prototype, {

          doTest : function(msg){
            alert("job collection do Test:"+msg);
          }

        });

        return Collection;
    }
}

alloy.js

var jobs = Alloy.createCollection('Job');
jobs.setAdapter('restapi');
jobs.fetch();
Ti.API.info("job count before:"+jobs.length);

// handle response from jobs fetch
jobs.on('fetch', function(){
  Ti.API.info("jobs on change start");
  Ti.API.info("jobs response:"+JSON.stringify(jobs));
  Ti.API.info("jobs count after:"+jobs.length);

  for (var i=0; i<jobs.length; i++) {
    Ti.API.info("job "+JSON.stringify(jobs[i]));
  }
});

Response

[{"account_id":"1","address1":"123 Market Street","address2":"","city":"San Diego","company_name":"Knotty Barrel","country":"United States","created_at":"2013-03-21T09:34:24Z","id":"84c149ae-920a-11e2-9917-dc2b61fffee2","job_id":null,"postal_code":"92101","state":"CA","updated_at":"2013-03-21T09:34:24Z"},{"account_id":"1","address1":"800 The Mark Lane","address2":"Unit 1703","city":"San Diego","company_name":"The Mark","country":"United States","created_at":"2013-03-21T09:34:42Z","id":"8f69964a-920a-11e2-9917-dc2b61fffee2","job_id":null,"postal_code":"92101","state":"CA","updated_at":"2013-03-21T09:34:42Z"}]

Log

[INFO] [iphone, 6.1, 192.168.1.139] SyncHistory.getJobs() count:2
[INFO] [iphone, 6.1, 192.168.1.139] job undefined
[INFO] [iphone, 6.1, 192.168.1.139] job undefined
viezel commented 11 years ago

@kevbaker Hi there. Im not really sure how to help you out. I think you should try the QA instead. The issues section here is for bugs/features in the adapter, not support :)

kevbaker commented 11 years ago

Hello Viezel,

Sorry I meant to update this ticket. I figured this out right after I posted it. I was iterating over the collection incorrectly. switched to model.each and everything is happy.

Thanks so much for this project. Amazing!!!

viezel commented 11 years ago

glad it worked out!

kevbaker commented 11 years ago

One last quick note I thought I'd mention.

Rather than using the restsql project, I opted for dynamically switching the Sync Adapter, this.config.adapter.type during runtime between "restapi" and "sql". Works like a charm.

This allows me to use the same models for restapi and sql and just swap out where to store the data. That in combination with a little syncmanager is working pretty well.

viezel commented 11 years ago

sounds great.. You should open source it. I have the restsql adapter aswell. I would be interested in your sync manager approach.

kevbaker commented 11 years ago

I will def be open sourcing it. We'll see where it ends up over the next couple weeks.

On Fri, Mar 22, 2013 at 12:42 AM, Mads Møller notifications@github.comwrote:

sounds great.. You should open source it. I have the restsql adapter aswell. I would be interested in your sync manager approach.

— Reply to this email directly or view it on GitHubhttps://github.com/viezel/napp.alloy.adapter.restapi/issues/6#issuecomment-15284666 .

viezel commented 11 years ago

Cool.. please let me know when you do.. :)