我正在使用ember-cli设置一个基本的应用程序,并且使用ember-data与api-stub遇到麻烦.我引用了api-stub README,并引用了ember指南,但无法弄清楚我缺少什么.我是一个noob,所以我原谅了任何明显的疏忽.
这是我的设置…
/api-stub/routes.js
server.get('/listings',function(req,res) { var listing = { "listing": [{ "id": 1,"title": "Sunny 1-bedroom","unit_type": "1br / 1ba","description": "Roomy 1-bedroom apartment in pre-war walkup. Gets great morning light from the south." },{ "id": 2,"title": "Large 2-bedroom","unit_type": "2br / 1.5ba","description": "Roomy 2-bedroom apartment in pre-war walkup. Gets great morning light from the south." }] }; res.send(listing); });
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({ namespace: 'api' }); export default ApplicationAdapter;
/package.json
{ ... "APIMethod": "stub",... }
/app/router.js
this.resource('listings',function() { this.resource('listing',{ path: '/:listing_id' }); });
/app/routes/listings.js
var ListingsRoute = Ember.Route.extend({ model: function() { return this.store.findAll('listing'); } }); export default ListingsRoute;
/app/models/listing.js
var attr = DS.attr,hasMany = DS.hasMany,belongsTo = DS.belongsTo; var Listing = DS.Model.extend({ title: attr(),unit_type: attr(),description: attr() }); export default Listing
/app/templates/listing.hbs
<h2>{{title}}</h2> <p>{{unit_type}}</p> <p>{{description}}</p>
在控制台中,它显示了一个404 / … / api /列表,并且chrome中的ember检查器没有显示任何记录.
任何帮助非常感谢!
解决方法@H_404_38@
截止到目前为止,ember-cli现在支持API stubbing.我也使用以下示例设置(非常类似于您的原始设置):
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({namespace: 'api'});
export default ApplicationAdapter;
/app/package.json
{
...
"APIMethod": "stub",...
}
/app/routes/application.js
export default Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
foos: this.store.findAll('foo'),bars: this.store.findAll('bar')
});
},setupController: function(controller,models) {
controller.set('foos',models.foos);
controller.set('bars',models.bars);
}
});
/app/router.js
var Router = Ember.Router.extend({
location: ENV.locationType
});
Router.map(function() {
this.resource('foos',function() {
this.resource('foo',{ path: '/:foo_id' });
});
this.resource('bars',function() {
this.resource('bar',{ path: '/:bar_id' });
});
});
export default Router;
/app/server/routes/foos.js
module.exports = function(app) {
app.get('/api/foos',res) {
res.send({
'foos': [
...
]
});
})
}
/app/server/routes/bars.js
module.exports = function(app) {
app.get('/api/bars',res) {
res.send({
'bars': [
...
]
});
})
}
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({namespace: 'api'}); export default ApplicationAdapter;
/app/package.json
{ ... "APIMethod": "stub",... }
/app/routes/application.js
export default Ember.Route.extend({ model: function() { return Ember.RSVP.hash({ foos: this.store.findAll('foo'),bars: this.store.findAll('bar') }); },setupController: function(controller,models) { controller.set('foos',models.foos); controller.set('bars',models.bars); } });
/app/router.js
var Router = Ember.Router.extend({ location: ENV.locationType }); Router.map(function() { this.resource('foos',function() { this.resource('foo',{ path: '/:foo_id' }); }); this.resource('bars',function() { this.resource('bar',{ path: '/:bar_id' }); }); }); export default Router;
/app/server/routes/foos.js
module.exports = function(app) { app.get('/api/foos',res) { res.send({ 'foos': [ ... ] }); }) }
/app/server/routes/bars.js
module.exports = function(app) { app.get('/api/bars',res) { res.send({ 'bars': [ ... ] }); }) }