return json to client with {ok, xxx} not {json, xxx}

373 views
Skip to first unread message

Caiyun Deng

unread,
Aug 14, 2014, 4:24:56 AM8/14/14
to chica...@googlegroups.com
Hi!
In the controller, "TestInfos = [[{name, "test1"}], [{name, "test2"}]], {ok, [{test_infos, TestInfos}]"

In the javacript, the value is not json array.
$(document).ready(function(){
    console.log("*******", {{ test_infos }});
It will  "Uncaught SyntaxError: Unexpected token , " --> console.log("*******", {name,"test1"}{name,"test2"});

I know i can return {json, [{test_infos, TestInfos]} to achieve this.
But i want to return by {ok, [{test_infos, TestInfos}]} and the client get {{ test_info }} is a json array, so i can use javascript to handle it in some particular situations, not to request the server one more time to get the value.

chan sisowath

unread,
Aug 14, 2014, 5:28:48 AM8/14/14
to chica...@googlegroups.com
hi,

you can use custom filter: 

src/view/lib/filter_modules/<app_name>_custom_filters.erl

to_json(Record) when is_tuple(Record) ->
    Type = element(1, Record),
    boss_json:encode(Record:attributes(), [Type]);
to_json(Proplists) ->
    boss_json:encode(Proplists, []).


in your controller:

action('GET', []) ->
Condition = [],
Record = boss_db:find_first(my_model_name, Conditions),
{ok, [{record, Record}]).




in your template:
<script>

{% autoescape off %}
var data1 ={{ Record | to_json }};

var data2 ={{ Record.attributes | to_json }};
{% endautoescape %}

</script>


should work , you can easily modify it to your need.
Chan




--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/178cb911-a409-4dee-a7e0-e3ecf0ee8acf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages