将多条记录添加到模型's系列帆船
Add multiple records to model's collection Sailsjs
我的Sailsjs应用程序中有以下具有多对多关系的模型:
event.js:
attributes: {
title : { type: 'string', required: true },
description : { type: 'string', required: true },
location : { type: 'string', required: true },
maxMembers : { type: 'integer', required: true },
currentMembers : { collection: 'user', via: 'eventsAttending', dominant: true },
creator : { model: 'user', required: true },
invitations : { collection: 'invitation', via: 'eventID' },
tags : { collection: 'tag', via: 'taggedEvents', dominant: true },
lat : { type: 'float' },
lon : { type: 'float' },
},
tags.js:
attributes: {
tagName : { type: 'string', unique: true, required: true },
taggedEvents : { collection: 'event', via: 'tags' },
},
根据文档,这种关系看起来是正确的。我在tag.js中有以下方法,它接受一个标记字符串数组和一个事件id,并应该添加或删除传入的标记:
modifyTags: function (tags, eventId) {
var tagRecords = [];
_.forEach(tags, function(tag) {
Tag.findOrCreate({tagName: tag}, {tagName: tag}, function (error, result) {
tagRecords.push({id: result.id})
})
})
Event.findOneById(eventId).populate('tags').exec(function(error, event){
console.log(event)
var currentTags = event.tags;
console.log(currentTags)
delete currentTags.add;
delete currentTags.remove;
if (currentTags.length > 0) {
currentTags = _.pluck(currentTags, 'id');
}
var modifiedTags = _.pluck(tagRecords, 'id');
var tagsToAdd = _.difference(modifiedTags, currentTags);
var tagsToRemove = _.difference(currentTags, modifiedTags);
console.log('current', currentTags)
console.log('remove', tagsToRemove)
console.log('add', tagsToAdd)
if (tagsToAdd.length > 0) {
_.forEach(tagsToAdd, function (tag) {
event.tags.add(tag);
})
event.save(console.log)
}
if (tagsToRemove.length > 0) {
_.forEach(tagsToRemove, function (tagId) {
event.tags.remove(tagId)
})
event.save()
}
})
}
这就是从事件模型调用方法的方式:
afterCreate: function(record, next) {
Tag.modifyTags(tags, record.id)
next();
}
当我发布到事件/创建时,我会得到以下结果:http://pastebin.com/PMiqBbfR.
看起来方法调用本身是循环的,而不仅仅是tagsToAdd或tagsToRemove数组。更令人困惑的是,在事件的最后一个日志中,事件似乎有正确的标签。然而,当我发布到event/1时,标记数组是空的。我也尝试过在每次.add()
之后立即保存,但仍然得到了类似的结果。
理想情况下,我希望循环遍历tagsToAdd和tagsToRemove数组,在模型的集合中修改它们的id,然后在模型上调用.save()
一次。
我已经花了很多时间试图调试这个,所以任何帮助都将不胜感激!
您的实现有一些问题,但主要问题是您将某些方法(即.save()
和.findOrCreate
)视为同步方法,而它们(与所有Waterline方法一样)是异步需要回调。因此,您可以有效地并行运行一堆代码,而不是等待它完成后再返回。
此外,由于您想要做的似乎是用这个新列表替换当前事件标记,因此您提出的方法有点过于设计了——您不需要使用event.tags.add
和event.tags.remove
。您可以使用普通的旧update
。
因此,您可能会将modifyTags
方法重写为:
modifyTags: function (tags, eventId, mainCb) {
// Asynchronously transform the `tags` array into an array of Tag records
async.map(tags, function(tag, cb) {
// For each tag, find or create a new record.
// Since the async.map `cb` argument expects a function with
// the standard (error, result) node signature, this will add
// the new (or existing) Tag instance to the resulting array.
// If an error occurs, async.map will exit early and call the
// "done()" function below
Tag.findOrCreate({tagName: tag}, {tagName: tag}, cb);
}, function done (err, tagRecords) {
if (err) {return mainCb(err);}
// Update the event with the new tags
Event.update({id: eventId}, {tags: tagRecords}).exec(mainCb);
});
}
请在此处查看async.map
的完整文档。
如果您想坚持使用.add
和.remove
来实现,那么您仍然希望使用async.map
,并在done
方法中执行其余的逻辑。您不需要两个.save
呼叫;只需先运行所有的.add
和.remove
代码,然后再运行一个.save(mainCb)
即可完成
我不知道你想通过从currentTags
中删除.add
和.remove
方法来实现什么(这是对event.tags
的直接引用),但它不起作用,以后只会引起混乱!
- 在VanillaJS中模拟模型双向数据绑定
- 为集合分配大量的模型弹药
- 如何使用backbone.js从集合中获取模型名称
- 动态更改高图中的系列颜色
- 骨干模型默认值-todos.js示例中不必要的代码
- EmberJS中支持单字母单词模型
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 将不在模型中的数据返回到mvc控制器
- 如何在视图模型contet更新更新上调用Jquery函数
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 我应该如何检查主干.主干.模型更改时查看
- Ext.js从json构建模型关系的问题
- 显示模块模式在Knockout中设置模型的新实例
- 使用导航属性创建Kendo UI网格模型的问题
- Angular,函数在(模型)工厂中返回值
- 如何将本地json数据加载到Extjs数据模型中
- 将多条记录添加到模型's系列帆船
- 制作一个模型的最佳方式'选择'Backbone.js系列中