ReferenceError:未定义fav

时间:2013-12-11 07:10:35

标签: ember.js

我是一个有emberjs的菜鸟,我现在已经坚持了一段时间。我用变量fav创建了一个itemController,但它一直说它没有定义。

我的index.html代码段如下所示:

<table class='table'>
                  {{#each model itemController='hotel'}}
                  <tr><td>
                      {{#if fav}}
                      <h4>{{title}}
                          <button {{action 'rmFav'}} type="button" class="btn btn-default btn-sm pull-right">
                            <span class="glyphicon glyphicon-heart"></span>
                          </button>
                      </h4>
                      {{else}}
                      <h4>{{title}}
                          <button type="button" class="btn btn-default btn-sm pull-right" {{action 'putFav'}}>
                          <span class="glyphicon glyphicon-heart-empty"></span>
                          </button>
                      </h4>
                      {{/if}}
                      <p> {{description}} </p>
                      <span class="glyphicon glyphicon-star"></span>
                      <span class="glyphicon glyphicon-star"></span>
                      <span class="glyphicon glyphicon-star"></span>
                      <span class="glyphicon glyphicon-star"></span>
                      <span class="glyphicon glyphicon-star-empty"></span>
                  </td></tr>
                  {{/each}}
              </table>

我的app.js有以下与itemController相关的片段:

App.HotelController = Ember.ObjectController.extend({

  actions: {
        putFav: function(){
            this.set(fav,true)
        },
        rmFav: function() {
            this.set(fav,false);
        }
    }
});

更新:只是澄清一下,我在app.js文件中有一个json格式的对象数组,其中的每个对象都包含fav属性。

1 个答案:

答案 0 :(得分:0)

您的问题是HotelController您在fav中引用了未声明的变量this.set(fav,...),您需要更改为字符串like this.set("fav",...)

App.HotelController = Ember.ObjectController.extend({

  actions: {
        putFav: function(){
            this.set("fav",true);
        },
        rmFav: function() {
            this.set("fav",false);
        }
    }
});