绑定模型更改为EmberJS中的视图

时间:2014-07-21 18:14:01

标签: javascript ember.js

我正在尝试使用EmberJS为记录器实现一个界面,并且很棒,但我碰到了一堵墙。

目前我有以下代码:

logs.hbs

...
{{input action="search" onEvent="keypress" value=searchText class="search" placeholder="Search"}}
...
<table id="log" class="log">
    <thead>
        <th>Service</th>
        <th>Start Time</th>
        <th>End Time</th>
        <th>URL</th>
        <th>Method</th>
        <th>User</th>
        <th>Response Code</th>
    </thead>
    <tbody>
        {{render "dashboard/logTableLine" model}}
    </tbody>
</table>
...

logTableLine.hbs

{{#each model}}
    {{#link-to "dashboard.log" _id tagName="tr"}}
        <td>{{ServiceID}}</td>
        <td>{{DateString StartTime}}</td>
        <td>{{DateString EndTime}}</td>
        <td>{{URL}}</td>
        <td>{{Method}}</td>
        <td>{{AuthName Auth}}</td>
        <td>{{StatusCode}}</td>
    {{/link-to}}
{{/each}}

和我的app.js

App = Ember.Application.create({
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: true
});

App.Router.map(function(){
    this.resource("dashboard", { path: "/" }, function(){
        this.route("logs", { path: "/logs" });
        this.route("log", { path: "/logs/log/:_id" });
    });
});

App.DashboardLogsRoute = Ember.Route.extend({
    model: function(){
        return Ember.$.getJSON("/logs.json");
    },
    actions: {
        search: function(value){
            if(value != '')
                this.set("content", Ember.$.getJSON("/logs.json?search=" + value));
        }
    }
});

App.DashboardLogRoute = Ember.Route.extend({
    model: function(params){
        return Ember.$.getJSON("/logs/log/" + params._id + ".json");
    }
});

我的问题是将该模型绑定到视图,以便在搜索调用服务器后视图重新呈现。我想搜索所有数据,而不仅仅是我在界面中的内容(最后100条记录)。

那么,第一个问题:为什么视图没有更新(绑定)到模型? 第二:有没有更好的方法(最好的实践)呢?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

现在您正在实施搜索而不会触发重新路由。因此,如果用户搜索并希望在他们搜索之前返回页面,他们将无法做到这一点。您要做的是将用户重新路由到同一页面,但传递查询参数。然后在模型函数中,您可以看到传入了哪些参数并相应地获取数据。 Ember的最新和最好的方法是查询参数功能:http://emberjs.com/guides/routing/query-params/它已经有一段时间了,现在处于测试阶段。我试一试,因为这是解决这个问题的一种非常干净,直观的方式。祝你好运!