How do I setup the api-stub in an ember-cli app?


#1

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;

/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!

I’ve also put this up on Stackoverflow, and will make sure to cross post any answers.


#2

hey, sorry for the confusion. The API Stub stuff in the cli doesn’t work yet. Hopefully it (or something similar) will work soon.

related issue: https://github.com/stefanpenner/ember-cli/issues/153


#3

Api-stub in Ember App Kit should be working just fine. You could start your app using app kit and then migrate to ember-cli when it becomes production ready.


#4

@hayvok thanks. I’ve started messing around with it and think it’s probably the best option to actually get some work done while Ember-CLI continues to get fleshed out.