derby -自动“刷新”在变化

derby - Automatic "refresh" on changes

本文关键字:变化 刷新 自动 derby      更新时间:2023-09-26

我正在尝试使用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>

应用程序只有"/"路由,它应该订阅所有的帖子。相反,回调只在第一次从数据库加载帖子时调用,而不是在任何更改时调用:

// 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","");
    }
})

"getAllPosts ()"-motif在服务器index.js文件中定义:

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

目前发生的是,当我按下"添加"按钮,一个新的帖子被添加到数据库中,但我只能在手动页面刷新后看到列表中的帖子。如果我在两个单独的标签中打开页面2次,并在一个标签中添加新帖子,则新帖子不会自动显示在另一个标签中。

我错过什么了吗?

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

尝试将<postList:>更改为:

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

我还添加了as :post以确保路径是正确的。这避免了您可能遇到的许多错误。

如果这还不能解决问题,或者你还有任何问题,请加入freenode上的#derbyjs并给我(switz)留言。