Amplify POST signature not working

153 views
Skip to first unread message

mandar katre

unread,
Feb 22, 2013, 8:02:51 AM2/22/13
to ampl...@googlegroups.com
Hi Experts, 

I am using the below code passing a hash to the resourceId and it returns me a message level of fatal, the model data at server is not populated, but when I use the other signature of request, its working fine and model gets populated properly. Please provide some suggestions.

amplify.request.decoders.appEnvelope =
    function (data, status, xhr, success, error) {
        if (data.status === "success") {
            success(data.data);
        } else if (data.status === "fail" || data.status === "error") {
            error(data.message, data.status);
        } else {
            error(data.message, "fatal");
        }
    };

amplify.request.define("decoderExample", "ajax", {
                url: "api/products",
                type: "POST",
                decoder: "appEnvelope"
            });

            amplify.request({
                resourceId: "decoderExample",
                success: function (data) {
                    debugger;
                },
                error: function (message, level) {
                    debugger;
                },
            }, 
            {
                "Id": "2",
                "Name": "Bag It",
                "Category": "Toys",
                "Price": "1"            
            });

mandar katre

unread,
Feb 22, 2013, 8:43:19 AM2/22/13
to ampl...@googlegroups.com
The following piece of code worked when i pass the data in the resourceId hash.. I was wondering if amplify POST doesnt work when data is passed in the data parameter. Is this a bug in amplify.js?

amplify.request.define("decoderExample", "ajax", {
                url: "api/products",
                type: "POST",
            });

            amplify.request({
                resourceId: "decoderExample",
                data: {
                    "Id": "2",
                    "Name": "Bag It",
                    "Category": "Toys",
                    "Price": "1"
                },
                success: function (data, status) {
                    debugger;
                },
                error: function (message, level) {
                    debugger;
                },
            });

Scott González

unread,
Feb 22, 2013, 8:54:43 AM2/22/13
to ampl...@googlegroups.com
No, it's a bug in your code. amplify.request() has two definitions and you were mixing them up. Please see http://amplifyjs.com/api/request/#usage for more information.


            });

--
You received this message because you are subscribed to the Google Groups "Amplify" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amplifyjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mandar katre

unread,
Feb 22, 2013, 9:08:15 AM2/22/13
to ampl...@googlegroups.com
But it is so confusing, how will one know that we have to pass the json data in the resourceId hash itself and not in data param for POST and PUT requests?
And when I pass resourceId as string, I can pass json data in the data parameter. This thing is not well documented in the amplify.js home site.


But anyways, thanks Scott, I appreciate your help and I have been reading a lot of other replies of yours on different posts which are awesome!

Scott González

unread,
Feb 22, 2013, 9:18:27 AM2/22/13
to ampl...@googlegroups.com
Can you clarify what is not clear about this?

P.S. Please stop calling it a resourceId hash, there is no such thing and it just adds to the confusion.


amplify.request( string resourceId [, hash data [, function callback ]] )

Request a resource.

  • resourceId: Identifier string for the resource.
  • data: A set of key/value pairs of data to be sent to the resource.
  • callback: A function to invoke if the resource is retrieved successfully.
amplify.request( hash settings )

Request a resource.

  • settings: A set of key/value pairs of settings for the request.
    • resourceId: Identifier string for the resource.
    • data (optional): Data associated with the request.
    • success (optional): Function to invoke on success.
    • error (optional): Function to invoke on error.


--

mandar katre

unread,
Feb 22, 2013, 9:30:17 AM2/22/13
to ampl...@googlegroups.com
I got the point where I went wrong. Just had a doubt - I can pass the successcallback as well as errorcallback only when I use the second signature by using the hash settings and in the first signature, I get just a callback, so we have to determine whether its successful or not using the status returned by the callback. I feel there should have been 2 callbacks in the first signature - a successcallback and an errorcallback ideally.

Scott González

unread,
Feb 22, 2013, 9:45:01 AM2/22/13
to ampl...@googlegroups.com
What you want isn't possible. Everything beyond the resource id is optional. It would be impossible to differentiate a success callback and an error callback. You should almost always use the hash form. The multi-param form is provided as sugar for people who like to do that kind of thing and ignore errors (which is a very large portion of developers).


On Fri, Feb 22, 2013 at 9:30 AM, mandar katre <katre....@gmail.com> wrote:
I got the point where I went wrong. Just had a doubt - I can pass the successcallback as well as errorcallback only when I use the second signature by using the hash settings and in the first signature, I get just a callback, so we have to determine whether its successful or not using the status returned by the callback. I feel there should have been 2 callbacks in the first signature - a successcallback and an errorcallback ideally.

--
Reply all
Reply to author
Forward
0 new messages