如何修改作为子模板中的数据上下文传递的数组的项?

时间:2016-01-27 13:25:10

标签: meteor meteor-blaze spacebars

我有一个存储赔率数组的本地集合:

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) {

2 个答案:

答案 0 :(得分:1)

我已经解决了这个问题!如果有人处于相同的情况,请使用以下代码:

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

答案 1 :(得分:0)

看起来你可以使用流星方法: 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);
      }
    )};