[Kimchi-devel] [PATCH 1/2] Add JS API for fetching i18n JSON

Hongliang Wang hlwang at linux.vnet.ibm.com
Thu May 15 08:54:26 UTC 2014


On 05/15/2014 08:25 AM, Adam King wrote:
> Create an API for fetching the i18n JSON.
> Use the API to load the kimchi UI strings.
>
> API will need to be simplified after convincing CherryPy to respond to the reque
> st for JSON.
>
> Signed-off-by: Adam King <rak at linux.vnet.ibm.com>
> ---
>   ui/js/src/kimchi.api.js  | 18 ++++++++++++++++++
>   ui/js/src/kimchi.main.js | 12 +++++++++---
>   2 files changed, 27 insertions(+), 3 deletions(-)
>
> diff --git a/ui/js/src/kimchi.api.js b/ui/js/src/kimchi.api.js
> index 7d85fdf..c20db2f 100644
> --- a/ui/js/src/kimchi.api.js
> +++ b/ui/js/src/kimchi.api.js
> @@ -63,6 +63,24 @@ var kimchi = {
>       },
>
>       /**
> +     * Get the i18 strings.
> +     */
> +    getI18n: function(suc, err, plugin) {
> +        if (plugin==undefined) {
> +            plugin="";
> +        }
> +        kimchi.requestJSON({
> +            url : kimchi.url + plugin + '/i18n.json',
> +            type : 'GET',
> +            resend: true,

> +            contentType : 'text/html', // 'application/json',
> +            dataType : 'text', //'json',  take the JSON directly rather than parsing after updating the CherryPy config.
contentType property can be ignored here because it is used only when we 
do a POST to  the server to inform the server the data sent is in JSON 
format.

dataType can be remained as "json" that dataType is used for jQuery and 
server-end doesn't need this property at all. If dataType is set to 
'json', jQuery will automatically parse the response text into a JS 
object and pass it into callback as a parameter. See 
http://api.jquery.com/jquery.ajax/
> +            success : suc,
> +            error: err
> +        });
> +    },
> +
> +    /**
>        * Get the host static information.
>        */
>       getHost: function(suc, err) {
> diff --git a/ui/js/src/kimchi.main.js b/ui/js/src/kimchi.main.js
> index 86fed5b..1b5aa62 100644
> --- a/ui/js/src/kimchi.main.js
> +++ b/ui/js/src/kimchi.main.js
> @@ -247,9 +247,15 @@ kimchi.main = function() {
>       };
>
>       // Load i18n translation strings first and then render the page.
> -    $('#main').load('i18n.html', function() {
> -        buildTabs(initUI);
> -    });
> +    kimchi.getI18n(
> +        function(i18nStrings){ //success
> +            i18n=jQuery.parseJSON(i18nStrings); //take the JSON directly rather than parsing after updating the CherryPy config.
> +            buildTabs(initUI);
> +            },
> +        function(data){ //error
> +            kimchi.message.error(data.responseJSON.reason);

> +            buildTabs(initUI);
If i18n strings is failed to load, we'd better inform user to retry 
instead of present a broken UI.
> +            });
>   };
>
>   kimchi.getHelp = function(e) {




More information about the Kimchi-devel mailing list