1

我正在关注Elixir lang上的Miguel Camba教程。

一切都很好,直到我尝试使用以下代码段:

得到“/play/:story_name”做
    conn = conn.resp_content_type("文本/事件流")
    conn = conn.send_chunked(200)

    iterator = File.iterator!("#{conn.params[:story_name]}.txt")

    Enum.each 迭代器,fn(line) ->
      { :ok, conn } = conn.chunk "数据:#{line}\n"
      等待连接, 1000, on_wake_up(&1, &2), on_time_out(&1)
    结尾
    康恩
  结尾

  defp on_wake_up(arg1, arg2) 做
    # 没有什么
  结尾

  defp on_time_out(arg1) 做
    # 没有什么
  结尾

我尝试了以下方法:

等待连接, 1000, on_wake_up&(&1, &2), on_time_out&(&1)
等待连接, 1000, on_wake_up(&(&1, &2)), on_time_out(&(&1))
等待连接,1000,on_wake_up(),on_time_out()
   结尾
    康恩
  结尾

  defp on_wake_up() 做
    # 没有什么
  结尾

  defp on_time_out() 做
    # 没有什么
  结尾

我想运行自省,看看分别传递给(arg1,arg2)和(arg1)的对象类型是什么,并且仍然试图弄清楚如何完成它。

同时,我无法判断快捷方式 & 号方法是否有用,因为我无法让它在 iex REPL 中工作。我的问题是,在这种情况下,您如何通过自省、文档或使用快捷 & 符号方法进行故障排除。提前致谢。

4

2 回答 2

5

语法已过时。您需要编写:&on_wake_up(&1, &2)&on_wake_up/2. 这些是匿名函数:

iex> is_function &is_atom/1
true
于 2014-03-08T00:12:43.573 回答
2

这是 Miguel Camba 教程的更新,还必须将cinderella.txtred-riding.txt文件添加到根目录:

application_router.ex

defmodule ApplicationRouter do
  use Dynamo.Router

  prepare do
    conn.fetch([:cookies, :params])
  end

  get "/" do
    conn = conn.assign(:title, "Welcome to Concurrent Story Teller!")
    render conn, "index.html"
  end

  get "/:file_name" do
    normalized_title = String.capitalize(String.replace(conn.params[:file_name], "-", " "))
    conn = conn.assign :title, normalized_title
    conn = conn.assign :file_name, conn.params[:file_name]
    render conn, "story.html"
  end

  get "/play/:story_name" do
    conn = conn.resp_content_type("text/event-stream")
    conn = conn.send_chunked(200)

    iterator = File.stream!("#{conn.params[:story_name]}.txt")

    Enum.each iterator, fn(line) ->
      { :ok, conn } = conn.chunk "data: #{line}\n"
      await conn, 1000, &on_wake_up(&1, &2), &on_time_out(&1)
    end
    conn
  end

  defp on_wake_up(arg1, arg2) do
    # Nothing
  end

  defp on_time_out(arg1) do
    # Nothing
  end

end


index.html.eex

<!DOCTYPE HTML>
<html>
<head>
  <title><%= @title %></title>
  <link href="/static/favicon.ico" rel="icon">
  <link href="/static/storyteller.css" media="all" rel="stylesheet" type="text/css">
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script ="/static/storyteller.js"></script>
</head>
<body>
    <div id="main-container">
        <h1><%=@title%></h1>
        <section class="stories-list">
            <h2>Please, choose a story</h2>
            <article class="story">
                <h3>Red Riding</h3>
                <a href="/red-riding">Read it to me!</a>
            </article>
            <article class="story">
                <h3>Cinderella</h3>
                <a href="/cinderella">Read it to me!</a>
            </article>
        </section>
    </div>
</body>
</html>


故事.html.eex

<!DOCTYPE HTML>
<html>
<head>
    <title><%= @title %></title>
    <link href="/static/favicon.ico" rel="icon">
    <link href="/static/storyteller.css" media="all" rel="stylesheet" type="text/css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="/static/storyteller.js"></script>
</head>
<body>
    <div id="main-container">
        <h1><%= @title %></h1>
        <section id="chat">

        </section>
        <a class="play" href="/play/<%= @file_name %>">Play!</a>
    </div>
</body>
</html>


讲故事的人.css

html, body {
  background-color: #4B7399;
  font-family: Helvetica, Verdana, Arial, sans-serif;
  font-size: 14px;
}

#main-container {
  width: 75%;
  margin: 0 auto;
  background-color: #FFF;
  padding: 20px 40px;
  border: solid 1px black;
  margin-top: 20px;
}
.stories-list .story{
  display: inline-block;
  width: 30%;
  margin: 10px 0.5%;
  padding: 0.5em;
  border: 1px solid lightgray;
  text-align: center;
}

@media only screen and (max-width: 600px) {
  .stories-list .story{ width: 90%; }
}
.story a{
  display: inline-block;
  background-color: #4B7399;
  text-decoration: none;
  color: white;
  padding: 5px;
  border-radius: 3px;
}
.story a:hover{
  background-color: #4E7FAC
}


讲故事的人.js

$(function(){
  $('.play').on('click', function(ev){
    ev.preventDefault();
    var source = new EventSource(this.href);
    source.addEventListener('message', function(e) {
      var $line = $('<p class="story-line">' + e.data + '</p>');
      $line.appendTo('#chat').hide().fadeIn(200, function(){
        $("html, body").animate({ scrollTop: $(document).height() }, "slow");
      })
    }, false);
  });
});
于 2014-03-08T11:43:13.667 回答