viezel / napp.alloy.adapter.restapi

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

Added hook for dynamic header values #35

Closed riteshdalal closed 10 years ago

nicka commented 10 years ago

:+1:

viezel commented 10 years ago

This feature has already been implemented:

see: https://github.com/viezel/napp.alloy.adapter.restapi/blob/master/restapi.js#L154-L161

and: https://github.com/viezel/napp.alloy.adapter.restapi/blob/master/restapi.js#L91-L94

nicka commented 10 years ago

I'm sorry didn't see this. Could you please give me some example code? Not sure how to do this dynamically. Thanks :+1:

riteshdalal commented 10 years ago

@nicka Check out the link below for example. https://github.com/riteshdalal/napp.alloy.adapter.restapi

You can also create each of the header properties as a function instead of a static value

headers : {
  "Cookie" : function(){
    //calculate a dynamic cookie value and set it
    var cookie_value = someFunctionThatFetchesIt();
    return "cookie_name="+cookie_value;
  }
}

@viezel There is a slight difference in what you provided and what I implemented. It is an enhancement that lets you create dynamic headers without knowing all the headers to set ahead of time and using what you pointed out, the headers could be a nested object

nicka commented 10 years ago

@riteshdalal @viezel managed to get it working. Didn't update to 1.1.5 yet. :see_no_evil:

viezel commented 10 years ago

@riteshdalal i dont follow. I cannot see how yours are more dynamic. You can do the following with my implementation

myModel.save({}, {
    headers : {
        "Cookie" : function(){
            var cookie_value = someFunctionThatFetchesIt();
            return "cookie_name="+cookie_value;
        },
        "AlloyUser": Alloy.Globals.getUserAPIKey();
    }
}