viezel / napp.alloy.adapter.restapi

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

add question mark if params.urlparams is empty #34

Closed vgrivel closed 10 years ago

vgrivel commented 10 years ago

Hi,

I'm just beginning with Restapi in titanium, and I have a "read" to perform without url parameter.

At the line 128, you set params.urlparams with the urlparams or an empty Json object.

Is the purpose of the line 200 is to test if params.urlparams is empty? If yes, it does not work. If no, how can I remove the "?" at the end of my url added by the function encodeData?

To test if a json object is empty, use the code:

function isEmpty(ob){
   for(var i in ob){
     return false;}
  return true;
}
viezel commented 10 years ago

you gotta show me some code cause I dont really follow you.

vgrivel commented 10 years ago

I will try to explain better ;-) Line 128:

//set default headers
params.headers = params.headers || {};

If you didn't define params.headers, it will have the value {} right?

Line 200

if (params.urlparams) {// build url with parameters
    params.url = encodeData(params.urlparams, params.url);
}

I think the purpose of the "if" is to test if params.urlparams is empty ( params.urlparams =={})? However, that test does not work.

If you want to test if an json object is empty, you have to use the function write in my previous message.

The point of my message is: if the params.urlparams contains only "{}", a question mark will be added in function "encodeData" because the if statement does not work.

To correct that, I replace the line 200 by:

if (!isEmpty(params.urlparams)) {// build url with parameters
    params.url = encodeData(params.urlparams, params.url);
}

...
function isEmpty(ob){
   for(var i in ob){
     return false;}
  return true;
}

Maybe it's me, cause I'm not yet a javascript guru.

viezel commented 10 years ago

you are mixing them up. params.headers and params.urlparams is not the same thing. headers are HTTP Headers and urlparams is url parameters: www.example.com/?param=hello

if (params.urlparams) {

is just to test if you have defined it in your fetch query (defined or null), like so:

col.fetch({
   urlparams:{ query: "hello world" }
});
vgrivel commented 10 years ago

sorry, I made a mistake in my question. The line 128 is

params.urlparams = params.urlparams || {};

and the line 110 is the params.headers.

However, if urlparams is not defined in the fetch, the question mark is added at the end of the url!

viezel commented 10 years ago

arh yes correct. I see that I failed to sync the restsql and restapi implementation. The bug is not present in the restsql (which is the one I use the most). Let me have a look at it.

viezel commented 10 years ago

Fixed in latest push

viezel commented 10 years ago

https://github.com/viezel/napp.alloy.adapter.restapi/blob/master/restapi.js#L172-L175