mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-02 12:00:51 +08:00
Decoders and cache and more tests
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Copied examples directly from AmplifyJs site
|
||||
|
||||
// Subscribe and publish with no data
|
||||
|
||||
amplify.subscribe("nodataexample", function () {
|
||||
@@ -66,3 +68,179 @@ amplify.store.sessionStorage("explicitExample", { foo2: "baz" });
|
||||
// retrieve the data later via the key
|
||||
var myStoredValue2 = amplify.store.sessionStorage("explicitExample");
|
||||
myStoredValue2.foo2; // baz
|
||||
|
||||
|
||||
// REQUEST
|
||||
|
||||
// Set up and use a request utilizing Ajax
|
||||
|
||||
|
||||
amplify.request.define("ajaxExample1", "ajax", {
|
||||
url: "/myApiUrl",
|
||||
dataType: "json",
|
||||
type: "GET"
|
||||
});
|
||||
|
||||
// later in code
|
||||
amplify.request("ajaxExample1", function (data) {
|
||||
data.foo; // bar
|
||||
});
|
||||
|
||||
// Set up and use a request utilizing Ajax and Caching
|
||||
|
||||
amplify.request.define("ajaxExample2", "ajax", {
|
||||
url: "/myApiUrl",
|
||||
dataType: "json",
|
||||
type: "GET",
|
||||
cache: "persist"
|
||||
});
|
||||
|
||||
// later in code
|
||||
amplify.request("ajaxExample2", function (data) {
|
||||
data.foo; // bar
|
||||
});
|
||||
|
||||
// a second call will result in pulling from the cache
|
||||
amplify.request("ajaxExample2", function (data) {
|
||||
data.baz; // qux
|
||||
})
|
||||
|
||||
// Set up and use a RESTful request utilizing Ajax
|
||||
|
||||
amplify.request.define("ajaxRESTFulExample", "ajax", {
|
||||
url: "/myRestFulApi/{type}/{id}",
|
||||
type: "GET"
|
||||
})
|
||||
|
||||
// later in code
|
||||
amplify.request("ajaxRESTFulExample",
|
||||
{
|
||||
type: "foo",
|
||||
id: "bar"
|
||||
},
|
||||
function (data) {
|
||||
// /myRESTFulApi/foo/bar was the URL used
|
||||
data.foo; // bar
|
||||
}
|
||||
);
|
||||
|
||||
// POST data with Ajax
|
||||
|
||||
amplify.request.define("ajaxPostExample", "ajax", {
|
||||
url: "/myRestFulApi",
|
||||
type: "POST"
|
||||
})
|
||||
|
||||
// later in code
|
||||
amplify.request("ajaxPostExample",
|
||||
{
|
||||
type: "foo",
|
||||
id: "bar"
|
||||
},
|
||||
function (data) {
|
||||
data.foo; // bar
|
||||
}
|
||||
);
|
||||
// Using data maps
|
||||
|
||||
// When searching Twitter, the key for the search phrase is q.If we want a more descriptive name, such as term, we can use a data map:
|
||||
|
||||
amplify.request.define("twitter-search", "ajax", {
|
||||
url: "http://search.twitter.com/search.json",
|
||||
dataType: "jsonp",
|
||||
dataMap: {
|
||||
term: "q"
|
||||
}
|
||||
});
|
||||
|
||||
amplify.request("twitter-search", { term: "amplifyjs" } );
|
||||
|
||||
// Similarly, we can create a request that searches for mentions, by accepting a username:
|
||||
|
||||
amplify.request.define("twitter-mentions", "ajax", {
|
||||
url: "http://search.twitter.com/search.json",
|
||||
dataType: "jsonp",
|
||||
dataMap: function (data) {
|
||||
return {
|
||||
q: "@" + data.user
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
amplify.request("twitter-mentions", { user: "amplifyjs" });
|
||||
|
||||
// Setting up and using decoders
|
||||
|
||||
//Example:
|
||||
|
||||
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: "/myAjaxUrl",
|
||||
type: "POST",
|
||||
decoder: "appEnvelope"
|
||||
});
|
||||
|
||||
amplify.request({
|
||||
resourceId: "decoderExample",
|
||||
success: function (data) {
|
||||
data.foo; // bar
|
||||
},
|
||||
error: function (message, level) {
|
||||
alert("always handle errors with alerts.");
|
||||
}
|
||||
});
|
||||
|
||||
// POST with caching and single - use decoder
|
||||
|
||||
// Example:
|
||||
|
||||
amplify.request.define("decoderSingleExample", "ajax", {
|
||||
url: "/myAjaxUrl",
|
||||
type: "POST",
|
||||
decoder: 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({
|
||||
resourceId: "decoderSingleExample",
|
||||
success: function (data) {
|
||||
data.foo; // bar
|
||||
},
|
||||
error: function (message, level) {
|
||||
alert("always handle errors with alerts.");
|
||||
}
|
||||
});
|
||||
// Handling Status
|
||||
// Status in Success and Error Callbacks
|
||||
|
||||
// amplify.request comes with built in support for status.The status parameter appears in the default success or error callbacks when using an ajax definition.
|
||||
|
||||
amplify.request.define("statusExample1", "ajax", {
|
||||
//...
|
||||
});
|
||||
|
||||
amplify.request({
|
||||
resourceId: "statusExample1",
|
||||
success: function (data, status) {
|
||||
},
|
||||
error: function (data, status) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Vendored
+3
-1
@@ -51,7 +51,9 @@ interface amplifyRequest {
|
||||
* error: Callback to invoke on error.
|
||||
*/
|
||||
define(resourceId: string, resource: Function): void;
|
||||
|
||||
|
||||
decoders: any;
|
||||
cache: any;
|
||||
}
|
||||
|
||||
interface amplifySubscribe {
|
||||
|
||||
Reference in New Issue
Block a user