问题描述
我正在使用ember-cli设置一个基本的应用程序,并且使用ember-data与api-stub遇到麻烦。我引用了api-stub README,并引用了ember指南,但无法弄清楚我缺少什么。我有点noob,所以原谅我的任何明显的疏忽。这是我的设置...
/api-stub/routes.js
server.get('/ listing',function(req,res ){
var listing = {
listing:[{
id:1,
title:Sunny 1-bedroom,
unit_type:1br / 1ba,
description:战前的卧室一卧室公寓从南部获得了很棒的早晨灯
},
{
id:2,
title:Large 2-bedroom,
unit_type:2br / 1.5ba,
description
}]
};
res.send(上市);
} );
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({
namespace:'api'
});
导出默认ApplicationAdapter;
/package.json
{
...
APIMethod:stub,
...
}
/app/router.js
resource('listing',function(){
this.resource('listing',{path:'/:listing_id'});
});
/app/routes/listings.js
$ $ c $ var vars = b}
});
导出默认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(),
描述:attr()
});
导出默认列表
/app/templates/listing.hbs
< h2> {{title}}< / h2>
< p> {{unit_type}}< / p>
< p> {{description}}< / p>
在控制台中,它显示了一个404 ... for ... / api / listings和chrome中的ember检查器显示没有记录
任何帮助非常感谢!
ember-cli现在支持API stubbing。我也使用以下示例设置(非常类似于您的原始设置):
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({namespace:'api'});
导出默认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'});
});
});
导出默认路由器;
/app/server/routes/foos.js
module.exports = function(app){
app.get('/ api / foos',function(req,res){
res。发送({
'foos':[
...
]
});
})
}
/app/server/routes/bars.js
module.exports = function(app){
app.get('/ api / bars',function(req,res){
res.send({
'酒吧':[
...
]
});
})
}
I’m setting up a basic app using ember-cli and am running into trouble with the api-stub with ember-data. I've referenced the api-stub README and have referenced the ember guides, but can't figure out what I'm missing. I’m a bit of a noob, so forgive any obvious oversights on my part.
Here's my setup...
/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>
In console it shows a 404 for …/api/listings and ember inspector in chrome is showing no records.
Any help much appreciated!
As of recently, ember-cli now supports API stubbing. I also got it working with the following example setup (very similar to your original setup):
/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', function(req, res) {
res.send({
'foos': [
...
]
});
})
}
/app/server/routes/bars.js
module.exports = function(app) {
app.get('/api/bars', function(req, res) {
res.send({
'bars': [
...
]
});
})
}
这篇关于如何在ember-cli应用程序中设置api-stub?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!