2017-04-04 60 views
0

我有一個API(我寫在ASP.Net Web API中)有一個終點,它返回一個狀態中的所有縣。EmberJS沒有通過我的動態路由段到api

我的路線應該是這樣的:http://localhost:4200/counties/CAhttp://localhost:4200/counties/NY

我的API是工作的罰款。

但是,我得到:Error while processing route: counties Ember Data Request GET http://localhost:50523/api/counties returned a 404

我的路由器映射:

// router.js 
Router.map(function() { 
    this.route('states'); 
    this.route('counties', {path : 'counties/:state'}); 
}); 

我也試過'counties/:fips' - FIPS是串行指定的PrimaryKey。它的地獄它和'counties/:county_id''counties/:state_id'

// routes/counties.js is: 
export default Ember.Route.extend({ 
    model(params) { 
    return this.store.query('county', {state: params.state}); 
    } 
}); 

// models/county.js: 
export default DS.Model.extend({ 
    state: DS.attr('string'), 
    countyName: DS.attr('string'), 
    fips: DS.attr('string'), 
}); 

請注意,狀態路由和API正在工作只是丹迪。

編輯:基於評論@LUX我意識到在測試路線之前,我需要更多的連線。所以,我修改了我的狀態模板中包含了像這樣列出的每個狀態的鏈接:

// templates/companies 
{{#each model as |state|}} 
<p>{{#link-to "counties" state.state}}{{state.state}} - {{state.stateName}}/link-to}}</p> 
{{/each}} 

我也是固定我的模型掛鉤(見上文)

現在我得到的狀態欄右邊的網址瀏覽器時懸停在鏈接上,但我仍然得到完全相同的錯誤。我再次檢查了所有的代碼,與我已經檢查過的其他兩個示例項目相比,我只是沒有看到這個問題。

編輯:

這裏是我的適配器:

// adapters/application.js 
import WebApiAdapter from 'ember-web-api/adapters/web-api'; 
import config from '../config/environment'; 

export default WebApiAdapter.extend({ 
    host: config.host, 
    namespace: 'api' 
}); 

回答

0

爲什麼會產生呢?路由器用於路由器,以及瀏覽器中的URL。不是您的API的網址。 動態段傳遞到您的model鉤:

model(params) { 
    console.log(params.state); 
} 

而且findAll是對給定類型獲取所有記錄。所以,你應該用query

model(params) { 
    return this.store.query('county', { state: params.state }); 
} 

現在,你必須在適配器可用的狀態,你可以作出適當的請求。

+0

讓我接近了,謝謝。我仍然失去了一些東西。查看修改。 –

+0

您必須從適配器返回正確的URL。你的適配器是什麼? – Lux

+0

我將我的適配器代碼添加到我的問題中。我需要一個不同於默認應用程序的適配器嗎?狀態路線工作正常,所以我認爲適配器都很好。 –