首先,您需要根据 thisfind
覆盖和create
蓝图。
第二,细看广告find
蓝图。为了实现您的目标,您需要更新查询条件并加入适当的(平均过滤)房间。
更新条件示例:
// Look up the model
var Model = actionUtil.parseModel(req),
criteria = actionUtil.parseCriteria(req),
user = req.user && req.user.id || 0,
allowed = user ? [0, user] : [0],
watchFilters = req.options.hasOwnProperty('watchFilters') ? req.options.watchFilters(req) : false,
classRoom = "sails_model_create_" + Model.identity;
// i need filter by authenticated user so forbidden all other requests
if (allowed.indexOf(user) === -1) {
return res.forbidden();
}
if (watchFilters) {
classRoom = _.reduce(Object.keys(watchFilters), function (classRoom, field) {
return classRoom + "_" + field + "_" + watchFilters[field];
}, classRoom);
}
if (_.result(Model, 'ownerFiltered')) {
criteria.user = user
}
再有一点watch
方法的单一调用,
if (req.options.autoWatch) { Model.watch(req); }
你需要用这个替换它:
if (req.options.autoWatch) {
sails.sockets.join(socket, classRoom);
sails.log.silly("Subscribed socket ", sails.sockets.id(req.socket), "to", classRoom, "filtered by", watchFilters);
}
为了加入适当的房间。
第三是用自己的调用替换publishCreate
蓝图create
中的broadcast
调用:
Model.broadcast(classRoom, Model.identity, {
verb: 'created',
data: data,
id: data[Model.primaryKey]
}, req.socket);
现在您可以使用以下内容过滤模型:
watchFilters: function (req) {
return {
user: req.user.id,
assembly: req.user.assembly || 1
};
},