使用结语,是否可以在没有关联的情况下获取资源?

时间:2016-02-24 01:52:55

标签: javascript associations sequelize.js epilogue

我有

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']
});

1 个答案:

答案 0 :(得分: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);