Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make entire response object available in load callback #9

Closed
natoverse opened this issue Mar 13, 2014 · 3 comments
Closed

Make entire response object available in load callback #9

natoverse opened this issue Mar 13, 2014 · 3 comments

Comments

@natoverse
Copy link
Contributor

Currently circuits unwraps the main data payload from a response JSON object, and sends it as the first argument to the load/success callback. The second argument gets an object with all of the params, the request, etc. We treat hypermedia links specially, and include them in this second argument as well. However, not all service APIs will have one main payload field on the response, and may want access to these secondary ones (just like "links", but not as a magic property). While it is very convenient to simply get the main payload as the first arg, leaving others unavailable is not desirable. We should add a "response" property to this second argument (to match the "request), so the raw data can be accessed.

Note that it is currently possible to work around this issue by using a "response" type plugin to gain access to the raw data during the lifecycle. This issue is primarily to make this more convenient so it is all available in the same callback.

@natoverse
Copy link
Contributor Author

More background on this:

When you call a service method via circuits, you get a very specific set of items passed to your callback. This was originally done to hide some of the ajax complexity, but we've found that there are cases where you will want access to the raw data, and having to wait for us to update the library design isn't useful. So there should be a mechanism where more data is passed to the callback.

Calling a circuits method looks something like:

var service = factory.getServiceByName('MyService');
//assuming the schema has a 'readDocument' method, which returns JSON like:
{
    "data": {
        "id": 1,
        "text": "hi"
    },
    "links": [{
        "rel": "self",
        "href": "documents/1"
    }]
}
service.readDocument({
    id: 1
}, {
    success: function (document, args) {
        //The first argument, 'document', contains only the unwrapped portion from the 'data' field:
        console.log('got document: ' + document.id); // "got document 1"

        //The second argument, 'args', contains a bunch of stuff we've packed on, including the original params, the request, links array, and so on. 
        console.log(args); // { id: 1, links: [...], plugin: {...}, request: {...}
    }
});

The 'args' param in the success callback should include a response field that contains the raw JSON.

@wastevus
Copy link
Contributor

So, in the exampe presented, we should have:

args.response = {
    "data": {
        "id": 1,
        "text": "hi"
    },
    "links": [{
        "rel": "self",
        "href": "documents/1"
    }]
}

am I right?

@natoverse
Copy link
Contributor Author

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants