2013-04-04 46 views
3

不幸的是,Ember指南中的鏈接示例(http://emberjs.com/guides/templates/links/)對我來說不起作用。linkTo生成一個到/ <App.Object:ID>的href,而不是/ ID

要顯示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個transitionTo另一個視圖之間的區別,在參考解析從列表中的單個對象?

3)如果是的話,何時會重新加載列表中的時刻,更新列表中的項目?

4)如果不是,我將如何獲得鏈接生成像/ tempworkers/[UUID]'這樣的href,並讓應用程序進行服務器API調用以獲取tempworker的詳細信息?

在此先感謝您的時間!

賴。

回答

2

你做得對,你剛剛犯了一個錯字,你的路線需要是/tempworker/:tempworker_id

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

Ember在尋找要反序列化的模型時使用提供的動態鍵。

+2

只是爲了澄清,因爲這對於來自Rails的用戶來說尤其不合常理,您必須使用/: _id約定,並且無法將其自定義爲僅使用/:id。 – evanbikes 2013-08-15 19:36:41

相關問題