I'm prototyping a small app using Ember-cli and it's http-mock feature. I can read records, and retrieve async associations just fine from the mock. That part works very well. However I'm running into trouble when I try to create, edit, or delete a record. Specifically, the record.save()
promise is getting rejected. I'm not experienced enough yet to know exactly why this is happening. Hopefully someone out there has some practice with this. Any help is much appreciated. Using ember-cli 0.2.0, ember 1.10.0, and ember-data 1.0.0-beta.15
// adapters/applications.js
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
namespace: 'api',
coalesceFindRequests: true
});
// models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
comments: DS.hasMany('comment', async: true)
});
// controllers/posts/new.js
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
save: function () {
var _this = this;
var name = this.get('model.name');
var post = this.store.createRecord('post', {
name: name
});
post.save().then(function () {
_this.set('message', 'Record Saved');
}, function () {
_this.set('message', 'Promise Rejected')
});
}
}
});
// server/mocks/posts.js
module.exports = function(app) {
var express = require('express');
var postsRouter = express.Router();
var posts = [{
id: 1,
name: 'Foo',
comments: [11, 999]
},
{
id: 2,
name: 'Bar',
comments: [21, 22]
}];
postsRouter.get('/', function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.send({
'posts': posts
});
});
postsRouter.post('/', function(req, res) {
res.status(201).end();
});
postsRouter.get('/:id', function(req, res) {
console.log(req.params);
res.send({
'posts': {
id: req.params.id
}
});
});
postsRouter.put('/:id', function(req, res) {
res.send({
'posts': {
id: req.params.id
}
});
});
postsRouter.delete('/:id', function(req, res) {
res.status(204).end();
});
app.use('/api/posts', postsRouter);
};