Joe*_*Joe 9 ember.js ember-data
我正在使用ember-cli设置一个基本应用程序,并且使用带有ember-data的api-stub遇到了麻烦.我引用了api-stub README并引用了ember指南,但无法弄清楚我缺少什么.我有点像菜鸟,所以请原谅我的任何明显的疏忽.
这是我的设置......
/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);
});
Run Code Online (Sandbox Code Playgroud)
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api'
});
export default ApplicationAdapter;
Run Code Online (Sandbox Code Playgroud)
/package.json
{
...
"APIMethod": "stub",
...
}
Run Code Online (Sandbox Code Playgroud)
/app/router.js
this.resource('listings', function() {
this.resource('listing', { path: '/:listing_id' });
});
Run Code Online (Sandbox Code Playgroud)
/app/routes/listings.js
var ListingsRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('listing');
}
});
export default ListingsRoute;
Run Code Online (Sandbox Code Playgroud)
/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
Run Code Online (Sandbox Code Playgroud)
/app/templates/listing.hbs
<h2>{{title}}</h2>
<p>{{unit_type}}</p>
<p>{{description}}</p>
Run Code Online (Sandbox Code Playgroud)
在控制台中,它显示了404 for .../api/listing和chrome中的ember检查器没有显示任何记录.
任何帮助非常感谢!
小智 9
截至最近,ember-cli现在支持API存根.我也使用以下示例设置(非常类似于您的原始设置):
/app/adapters/application.js
var ApplicationAdapter = DS.RESTAdapter.extend({namespace: 'api'});
export default ApplicationAdapter;
Run Code Online (Sandbox Code Playgroud)
/app/package.json
{
...
"APIMethod": "stub",
...
}
Run Code Online (Sandbox Code Playgroud)
/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);
}
});
Run Code Online (Sandbox Code Playgroud)
/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;
Run Code Online (Sandbox Code Playgroud)
/app/server/routes/foos.js
module.exports = function(app) {
app.get('/api/foos', function(req, res) {
res.send({
'foos': [
...
]
});
})
}
Run Code Online (Sandbox Code Playgroud)
/app/server/routes/bars.js
module.exports = function(app) {
app.get('/api/bars', function(req, res) {
res.send({
'bars': [
...
]
});
})
}
Run Code Online (Sandbox Code Playgroud)
要配置存根 API,您需要编辑package.json。
stub使用这些快速服务器存根路由。http://localhost:8000更新 - 这是我的工作设置
api-stub/routes.js
server.namespace('/api', function() {
server.get('/posts', function(req, res) {
res.send([...]);
});
});
Run Code Online (Sandbox Code Playgroud)
package.json
...
"name": "myapp",
"namespace": "appkit",
"APIMethod": "stub",
"proxyURL": "http://localhost:8000",
"proxyPath": "/api",
...
Run Code Online (Sandbox Code Playgroud)
现在,ajax GET请求http://localhost:8000/api/posts以模拟数组进行响应。
希望这对你有用。