The controller (cb_tutorial_greeting_controller.erl):

-module(cb_tutorial_greeting_controller, [Req]).
-compile(export_all).

hello('GET', []) ->
    {ok, [{greeting, "Hello, world!"}]}.

list('GET', []) ->
    Greetings = boss_db:find(greeting, []),
    {ok, [{greetings, Greetings}]}.

create('GET', []) ->
    ok;
create('POST', []) ->
    GreetingText = Req:post_param("greeting_text"),
    NewGreeting = greeting:new(id, GreetingText),
    case NewGreeting:save() of
        {ok, SavedGreeting} ->
            {redirect, [{action, "list"}]};
        {error, ErrorList} ->
            {ok, [{errors, ErrorList}, {new_msg, NewGreeting}]}
    end.

goodbye('POST', []) ->
    boss_db:delete(Req:post_param("greeting_id")),
    {redirect, [{action, "list"}]}.

send_test_message('GET', []) ->
    TestMessage = "Free at last!",
    boss_mq:push("test-channel", TestMessage),
    {output, TestMessage}.

pull('GET', [LastTimestamp]) ->
    {ok, Timestamp, Greetings} = boss_mq:pull("new-greetings",
        list_to_integer(LastTimestamp)),
    {json, [{timestamp, Timestamp}, {greetings, Greetings}]}.

live('GET', []) ->
    Greetings = boss_db:find(greeting, []),
    Timestamp = boss_mq:now("new-greetings"),
    {ok, [{greetings, Greetings}, {timestamp, Timestamp}]}.


And the model (greeting.erl):

-module(greeting, [Id, GreetingText]).
-compile(export_all).

validation_tests() ->
    [{fun() -> length(GreetingText) > 0 end,
        "Greeting must be non-empty!"},
     {fun() -> length(GreetingText) =< 140 end,
        "Greeting must be tweetable"}].

before_create() ->
    ModifiedRecord = set(greeting_text,
                         re:replace(GreetingText,
                                    "masticate", "chew",
                                    [{return, list}])),
    {ok, ModifiedRecord}.

after_create() ->
    boss_mq:push("new-greetings", THIS).


Thanks
Romu

can2nac於 2014年1月14日星期二UTC+8下午10時05分09秒寫道:
>
> Actually 'data' is undefined here. --- i think this is the key, post 
> controller here.
>
> On Tuesday, January 14, 2014 12:32:50 PM UTC+2, Romu wrote:
>>
>> Hi,
>>
>> I was trying the example in CB tutorial "3. Implementing real-time 
>> updates with save hooks", below is my live.html:
>>
>> <html><head>
>> <title>Fresh hot greetings!</title>
>> <script src="
>> http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
>> "></script>
>> <script>
>>   function listen_for_events(timestamp) {
>>     $.ajax("/greeting/pull/"+timestamp, { success:
>>       function(data, code, xhr) {
>>         console.log(typeof data.greetings);
>>         console.log(typeof data.greetings !== 'undefined' ? 
>> data.greetings.length : 'There is no spoon.');
>>         for (var i=0; i<data.greetings.length; i++) {
>>           var msg = data.greetings[i].greeting_text;
>>           $("#greeting_list").append("<li>"+msg);
>>         }
>>         listen_for_events(data.timestamp);
>>       } });
>>   }
>>   $(document).ready(function() {
>>     listen_for_events({{ timestamp }});
>>   });
>> </script>
>> </head>
>> <body>
>>   <ul id="greeting_list">
>>   {% for greeting in greetings %}
>>     <li>{{ greeting.greeting_text }}
>>   {% empty %}
>>     <li>No greetings!
>>   {% endfor %}
>> </ul>
>> </body>
>> </html>
>>
>> When I create a new greeting, the live page does not update with the new 
>> greeting, in console I see javascript error:
>>
>> Uncaught TypeError: Cannot read property 'length' of undefined live:10
>> $.ajax.success        live:10
>> c                            jquery.js:3048
>> p.fireWith                jquery.js:3160
>> k                            jquery.js:8235
>> r                             jquery.js:8778
>>
>> Actually 'data' is undefined here.
>>
>> Browser is Chrome 32.0.1700.72 m.  I also tried jquery 1.70 and 2.0.3, 
>> same issue.
>>
>> Any idea?
>>
>>
>> Thanks
>> Romu
>>
>

-- 
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 [email protected].
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/babf8b75-76e7-4be9-a8c0-424fe7a290a4%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to