Ember:在控制器

时间:2015-10-26 23:24:20

标签: ember.js

我的模板中有一个名为 someString 的变量,我想在控制器中设置该变量。我收到未捕获错误:断言失败:TypeError:无法读取属性'设置'未定义

如何让控制器知道模板变量(如果这是解决此问题的方法)?

我的模板如下所示:



{{#modal-dialog action="close" title="New Run"}}
  <form role="form" {{action "submit" on="submit"}}>
...
  {{input value=emailComparisonRunID class="form-control" placeholder="Comparison Run ID (optional)"}}
  <label><small>{{someString}}</small></label>
  <br>
...
  </form>
{{/modal-dialog}}
&#13;
&#13;
&#13;

我的控制器看起来像这样:

&#13;
&#13;
export default Ember.ObjectController.extend({
  needs: 'services',
  dataVer: '',

  watchEmailComparisonRunId: function() {
    var comparisonId = this.get('emailComparisonRunID');
    if(comparisonId == null) {
      comparisonId = 1;
    }

    var onIdFound = function(id) {
      console.log('FOUND');
      this.set('someString', 'FOUND');
    };

    var onIdNotFound = function(reason) {
      console.log('Not Found');
      this.set('someString', 'NOT FOUND')
    };

    this.get('store').find('run', comparisonId).then(onIdFound, onIdNotFound);

  }.observes('emailComparisonRunID'),
  
  ...

  
  
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您在观察者中创建函数,这些函数使用错误的上下文调用。更新它们以绑定其功能的上下文:

var onIdFound = function(id) {
  console.log('FOUND');
  this.set('someString', 'FOUND');
}.bind(this);

var onIdNotFound = function(reason) {
  console.log('Not Found');
  this.set('someString', 'NOT FOUND')
}.bind(this);