vendredi 24 juillet 2015

Ember: working with http-mock and improper JSON

Trying to work with http-mock feature of Ember-cli. My plan is not input some improper JSON(without root model, no Ids, embedded model instead of side loaded model etc etc) . Since i am new to this i first tried setting up a mock server with proper data and it was successful. Below is the relevant code.

adapters/post.js

import DS from "ember-data";
export default DS.RESTAdapter.extend({
    namespace: 'api',
});

pods/post/route.js

import Ember from "ember";

export default Ember.Route.extend({
  model: function(){
    return this.store.find('post');
  }
});

models/post.js

import DS from 'ember-data';

export default  DS.Model.extend({
  author: DS.attr('string'),
  title: DS.attr('string'),
  body: DS.attr('string'),
 comments: DS.hasMany('comments')
});

server/mocks/posts.js

module.exports = function(app) {
  var express = require('express');
  var postsRouter = express.Router();

  postsRouter.get('/', function(req, res) {
    res.send({
      "posts" : [{
         'id': 1,
         'author': 'Brian',
         'title': 'Ember JS',
         'body': 'JS framework for creating ambitious web applications',
         'comments': [11,12]
       }]
    });
  });

  app.use('/api/posts', postsRouter);
};

This this point everything works fine. The above mock server data relates to the below JSON from an API call

{
    "posts" : [{
        'id': 1,
        'author': 'Brian',
        'title': 'Ember JS',
        'body': 'JS framework for creating ambitious web applications',
        'comments': [11,12]
    }]
}   

How would i write my mock-server code to produce a JSON which looks like this without root model?

{
    'id': 1,
    'author': 'Brian',
    'title': 'Ember JS',
    'body': 'JS framework for creating ambitious web applications',
    'comments': [11,12]
}       

Sorry for the long post and thanks in advance.

-Anees




Aucun commentaire:

Enregistrer un commentaire