derby - 自动“刷新”更改

时间:2013-01-08 09:01:41

标签: javascript node.js derbyjs

我正在尝试使用derbyjs,并且无法弄清楚使用订阅的实时更新是如何工作的。

目前,该应用程序只是一个尽可能基本的后期标题列表和最后一个可以添加新帖子的文本字段:

<Title:>
  Sample derby app

<Header:>
  <!-- This is a component defined in the /ui directory -->
  <ui:connectionAlert>

<Body:>
  <h1>Posts</h1>
  <app:postList>
  <input type="text" value="{_fieldValue}"><input type="button" value="add" x-bind="click:add">

<postList:>
    {{#each posts}}
        <app:post>
    {{/}}

<post:>
    <div>{{title}}</div>

该应用只有&#34; /&#34;路线,应该订阅所有帖子。相反,回调只是在第一次从数据库加载帖子时调用,而不是在任何更改时调用:

// Derby routes can be rendered on the client and the server
get('/', function(page, model, params) {
    model.subscribe(model.query("posts").allPosts(), function(err, posts) {
        page.render({
            posts:posts.get()
        })
    })
})


// CONTROLLER FUNCTIONS //

ready(function(model) {
    this.add = function(){
        model.set("posts."+model.id(),{title:model.get("_fieldValue")});
        model.set("_fieldValue","");
    }
})

&#34; getAllPosts()&#34; -motif在服务器index.js文件中定义:

store.query.expose("posts","allPosts",function(){
    return this;
});

当前发生的情况是,当我按下&#34;添加&#34; -Button时,新帖子会添加到数据库中,但我只能在手动页面刷新后看到列表中的帖子。如果我在2个单独的选项卡中打开页面2次并在一个选项卡中添加新帖子,则新帖子不会自动显示在其他选项卡中。

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

为了进行实时绑定,您需要使用单个括号。

尝试将<postList:>更改为:

<postList:>
    {#each posts as :post}
        <app:post>
    {/each}

<post:>
    <div>{:post.title}</div>

我还添加了as :post以确保路径正确。这样可以避免很多错误。

如果这不能解决问题,或者您有任何疑问,请在freenode上加入#derbyjs并给我发消息(switz)。