【问题标题】:How to keep your model in sync with an {{#each}} block?如何使您的模型与 {{#each}} 块同步?
【发布时间】:2014-10-09 03:19:21
【问题描述】:

我有这样的事情:

Template.todoList.helpers({
    todos: function() {
        return Todos.find({}); // Returns records with a todoText, ownerId and done field.
    }
});

然后在模板中我使用{{#each}} 块来列出待办事项。但如果他们是done,我希望能够通过复选框进行更改。如果我只是在{{#each}} 块中添加这样的复选框,它将正确显示初始状态,但如果我切换复选框,记录将不会更新。我需要跟踪记录的_id,但我应该将它存储在哪里?如果我能找到正确的_id,剩下的就很简单了:

Template.todoList.events({
    'change .doneCheckbox': function(event) {
        var todoId = ??;
        Todos.update(todoId, {$set: {done:event.target.checked}}); 
    }
});

我会在?? 的位置插入什么?

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    应该可以从this(更具体地说是this._id)获得:

    var todo = this,
        todoID = todo._id;
    

    您也可以从记录中访问其他属性(例如this.done)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-04
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2014-08-22
      • 1970-01-01
      相关资源
      最近更新 更多