21

我有

epilogue.resource({
  model: db.Question,
  endpoints: ['/api/questions', '/api/questions/:id'],
  associations: true
});

因此,当我点击 时/api/questions,我会取回与资源的所有关联。在某些情况下,我可以通过什么来获取关联吗?或者我应该创建一个新端点:

epilogue.resource({
  model: db.Question,
  endpoints: ['/api/questions2', '/api/questions2/:id']
});
4

1 回答 1

1

一种方法是使用里程碑,您可以在某些情况下为列表和读取行为定义里程碑,您可以访问 req 对象,以便您可以相应地进行更改

https://github.com/dchester/epilogue#customize-behavior

这是一个列表调用修改的例子

// my-middleware.js
module.exports = {
  list: {
    write: {
      before: function(req, res, context) {
        // modify data before writing list data
        return context.continue;
      },
      action: function(req, res, context) {
        // change behavior of actually writing the data
        return context.continue;
      },
      after: function(req, res, context) {
        // set some sort of flag after writing list data
        return context.continue;
      }
    }
  }
};

// my-app.js
var epilogue = require('epilogue'),
    restMiddleware = require('my-middleware');

epilogue.initialize({
    app: app,
    sequelize: sequelize
});

var userResource = epilogue.resource({
    model: User,
    endpoints: ['/users', '/users/:id']
});

userResource.use(restMiddleware);
于 2017-01-24T05:50:52.630 回答