1

REST service

Hello

Just started to look over mojito, quite a lot to cover. The first thing i would like to implement is a REST service. How i can bind a controller for a example to a json view.

by
4 Replies
  • In an action (at the controller), you can just do:

    ac.done(data, 'json');

    That will stringify the data structure, and add the proper headers.

    If you want to do it manually, by executing an actual view for example. You can do this:

    ac.http.addHeader('Content-Type', 'application/json; charset="utf-8"');
    ac.done(data, {
        view: {
           name: 'api-json-view-whatever'
        }
    });

    So, the result of the done call (will be a string), and it will be sent to the client side as "application/json".

    Or maybe a jsonp response:

    ac.http.addHeader('Content-Type', 'application/javascript; charset="utf-8"');
    ac.done(cb+'('+JSON.stringify(data)+')');
    0
  • For the second case when I specify the view name. What would be the best approach to implement the view? Should I do it using the moustache template framework? What would be the convention on the file name? 
    0
  • By default, you can just use a regular mojito view (which is a mustache template). More info here:

    http://developer.yahoo.com/cocktails/mojito/docs/code_exs/simple_view_template.html

    Put the view as:
    <myMojit>/views/<name-of-the-action.mu.html

    Best regards,
    Caridy
    0
  • To access any REST Service.. You can use Mojito REST Api given here..
    http://developer.yahoo.com/cocktails/mojito/docs/api_overview/mojito_rest_lib.html
    0

Recent Posts

in Yahoo! Mojito