如何修改在子模板中作为数据上下文传递的数组项?
How do I modify the item of an array passed as data context in a child template?
我有一个存储赔率数组的本地集合:
ChecksCollection = new Mongo.Collection(null);
ChecksCollection.insert({
odds: ['', ''],
oddsAverages: ['', ''],
oddsCompeting: ['', '']
});
以及设置父数据上下文的助手:
Template.Step1.helpers({
bet: () => ChecksCollection.findOne()
});
现在我需要一个子模板,它可以修改 ChecksCollection 中任何数组的任何索引。目前我是这样称呼它的:
{{>InputOdds odds=bet.odds.[0] label='The odds that are being offered to you:' placeholder='Any odds style works! Equivalent examples: 1/4 1.25 -400'}}
{{>InputOdds odds=bet.odds.[1] label='The odds offered for the opposite outcome:' placeholder=''}}
稍后我可能会用 {{#each ... in ...}} 调用它来循环遍历 oddsAverages。
这是子模板:
<template name="InputOdds">
<div class="form-group">
<label for="odds">{{label}}</label>
<input type="text" class="form-control" id="odds" value="{{odds}}"
placeholder="{{placeholder}}">
</div>
</template>
更简单地说,我的问题是要修改什么以及在下面几行之后写什么,以允许子模板更新数据上下文(因此调用 ChecksCollection 数组中的更新和修改值):
Template.InputOdds.events({
'keyup input.form-control': function (event, template) {
看起来你可以使用流星方法:
http://docs.meteor.com/#/full/meteor_methods
所以你可以这样做:
服务器:
Meteor.methods({
foo: function (arg1, arg2, arg2) {
ChecksCollection.insert({
odds: ['', ''],
oddsAverages: ['', ''],
oddsCompeting: ['', '']
});
}
)};
然后调用方法:
Template.InputOdds.events({
'keyup input.form-control': function (event, template) {
//set up your data from the form
Meteor.call('foo', data1, data2, data3);
}
)};
这个问题我已经解决了耶!如果有人遇到相同情况,请使用以下代码:
var objectForSet = {};
objectForSet[key + '.' + index] = value;
//todo remove debugging code from below. The line which creates the rendering problem is: ChecksCollection.update({}, {$set: objectForSet});
console.log(`Modifying local collection at key: ${key}; array index: ${index};`);
ChecksCollection.update({}, {$set: objectForSet});
我有一个存储赔率数组的本地集合:
ChecksCollection = new Mongo.Collection(null);
ChecksCollection.insert({
odds: ['', ''],
oddsAverages: ['', ''],
oddsCompeting: ['', '']
});
以及设置父数据上下文的助手:
Template.Step1.helpers({
bet: () => ChecksCollection.findOne()
});
现在我需要一个子模板,它可以修改 ChecksCollection 中任何数组的任何索引。目前我是这样称呼它的:
{{>InputOdds odds=bet.odds.[0] label='The odds that are being offered to you:' placeholder='Any odds style works! Equivalent examples: 1/4 1.25 -400'}}
{{>InputOdds odds=bet.odds.[1] label='The odds offered for the opposite outcome:' placeholder=''}}
稍后我可能会用 {{#each ... in ...}} 调用它来循环遍历 oddsAverages。 这是子模板:
<template name="InputOdds">
<div class="form-group">
<label for="odds">{{label}}</label>
<input type="text" class="form-control" id="odds" value="{{odds}}"
placeholder="{{placeholder}}">
</div>
</template>
更简单地说,我的问题是要修改什么以及在下面几行之后写什么,以允许子模板更新数据上下文(因此调用 ChecksCollection 数组中的更新和修改值):
Template.InputOdds.events({
'keyup input.form-control': function (event, template) {
看起来你可以使用流星方法: http://docs.meteor.com/#/full/meteor_methods
所以你可以这样做:
服务器:
Meteor.methods({
foo: function (arg1, arg2, arg2) {
ChecksCollection.insert({
odds: ['', ''],
oddsAverages: ['', ''],
oddsCompeting: ['', '']
});
}
)};
然后调用方法:
Template.InputOdds.events({
'keyup input.form-control': function (event, template) {
//set up your data from the form
Meteor.call('foo', data1, data2, data3);
}
)};
这个问题我已经解决了耶!如果有人遇到相同情况,请使用以下代码:
var objectForSet = {};
objectForSet[key + '.' + index] = value;
//todo remove debugging code from below. The line which creates the rendering problem is: ChecksCollection.update({}, {$set: objectForSet});
console.log(`Modifying local collection at key: ${key}; array index: ${index};`);
ChecksCollection.update({}, {$set: objectForSet});