Collection retreiving data from RESTful service that requires auth

Hi all!

I am writing some code to retrieve a data from a RESTful service. I am implementing that inside an Enyo.Collection
The web service i'm querying requires logging in before data can be retrieved.
Logging in is simply calling another API from the same service (say http://example.com/api/login)

That collection has a create and fetch methods.
I am calling the login as part of create method and i'm calling the actual collection fetching function as part of the fetch function implementation.

My trouble is that collection gets created and fetch is called immediately (like new MyCollection().fetch())
Because of that, by the time fetch executes, the call to api to login hasn't been done yet and i'm struggling to understand how they can be chained to ensure that we call and parse the output of the login procedure first, and only then call the fetch.

Enyo 2.4 is in use, so some nostalgy time (can't use newer enyo as need compatibility with old LG TVs)
Sample code:
enyo.kind({ kind: "enyo.Collection", name: "MyCollection", url:"https://myservice/api/getlist", defaultSource:"jsonp", create: function() { this.inherited(arguments); var requrl="http://myservice/api/login"; var req = new enyo.JsonpRequest({url:requrl}); req.response(this,function(inSender,inEvent){ if (inEvent.token) { this.set("token",inEvent.token); } }); req.go(); }, fetch: function(opts) { if (this.token) { return this.inherited(arguments); } } );

Code above is lame, but hope is enough to explain what i'm trying to achieve - i want the token to be populated by constructor before fetch is called. I know this is making stuff run synchronously and i'm open to suggestions on how this should be done.
Sign In or Register to comment.