linkTo生成一个/ <app.object:id>的href,而不是/ ID </app.object:id>

时间:2013-04-04 07:35:03

标签: ember.js ember-data

不幸的是,Ember指南中的链接示例(http://emberjs.com/guides/templates/links/)对我来说不起作用。

要显示一个tempworkers列表,并为每个tempworker链接到详细信息页面,我使用以下代码:

HTML:

<script type="text/x-handlebars" data-template-name="tempworkers">
  <div>
    {{#each controller }}  
      <tr>
        <td>{{#linkTo 'tempworker' this}} {{firstname}} {{/linkTo}}</td>
        <td>{{initials}}</td>
        <td>{{completeSurname}}</td>
      </tr>
    {{/each}}
  </div>
</script>

<script type="text/x-handlebars" data-template-name="tempworker">
  <div id="tempworker">
    <p>{{view Ember.TextField valueBinding='firstname'}}</p>
    <p>{{view Ember.TextField valueBinding='initials'}}</p>
    <p>{{view Ember.TextField valueBinding='surnamePrefix'}}</p>
    <p>{{view Ember.TextField valueBinding='surname'}}</p>
  </div>
</script>

使用Javascript:

window.App = Ember.Application.create({
    LOG_TRANSITIONS: true,
    ENV.EXPERIMENTAL_CONTROL_HELPER = true;
});

App.Store = DS.Store.extend({
    revision    : 11,
    adapter     : DS.RESTAdapter.extend({
        url     : 'http://dev.start.flexplanners.com/api'
    })
});

App.Router.map(function() {
    this.route('tempworkers'); // /tempworkers
    this.route('tempworker', { path: '/tempworkers/:id' }); // /tempworkers/:id
});

App.TempworkersRoute = Ember.Route.extend({
    model : function() {
        return App.Tempworker.find(); // return All tempworkers to the TempworkersController
    }
});

/* TODO: Question -> According to http://emberjs.com/guides/routing/defining-your-routes/, this should be working by convention, and NOT explicitely defined here?
 * But when I leave it out, it's broken :(
 * */
App.TempworkerRoute = Ember.Route.extend({
    model : function(params) {
        return App.Tempworker.find(params.id); // return tempworker by id to the TempworkerController
    }
});

用例:

When I visit the URL /tempworkers, the app fires a request 
to my server API, and return the list in my tempworkers view.

When I visit the URL /tempworkers/[UUID], the app fires a 
request to my server API, and return the requested tempworker 
in my tempworker view.

When I click a generated link in the list of tempworkers, the app routes me 
to /tempworkers/<App.Tempworker:ember343:47cfa9f2159d45758ceacc4c15ae1671>, 
and shows the details in my tempworker view

我的问题如下:

1)这3个用例是否表现出预期的行为?

2)如果是,这是否显示2个状态之间的区别,1是新的URL访问(包括服务器API调用),1是转换到另一个视图,从列表中解析引用单个对象? / p>

3)如果是,何时会重新加载列表,并更新列表中的项目?

4)如果不是,我怎样才能让linkTo生成像'/ tempworkers / [UUID]'这样的href,并让应用程序进行服务器API调用以获取tempworker的详细信息?

提前感谢您的时间!

1 个答案:

答案 0 :(得分:2)

你做对了,你刚刚写错了,你的路线需要/tempworker/:tempworker_id

App.Router.map(function() {
  this.route('tempworkers'); // /tempworkers
  this.route('tempworker', { path: '/tempworkers/:tempworker_id' }); // /tempworkers/:id
});

Ember在查找要反序列化的模型时使用提供的动态键。