如何观察模型中相关对象的状态?

时间:2013-12-03 11:43:08

标签: javascript ember.js

如何从电影模型中观察更改Session对象的状态?我需要这种方法来确定当前用户是否经过身份验证,以及每部电影的所有者是否允许编辑。属性有效,但不会对Session对象中的更改做出反应。现在,使用句柄模板中的if子句,Observed将无效。

到目前为止

代码:

App.Movie = Ember.Model.extend({
  title: Ember.attr(),
  year: Ember.attr(),

  owner: Ember.belongsTo('App.User', {
    key: 'owner',
    serializer: UserType
  }),

  canEdit: function() {
    var owner = this.get('owner');

    console.log(owner, App.Session.objectId, owner.get('objectId'));

    if (owner && App.Session.objectId === owner.get('objectId')) {
      return true;
    }

    return false;
  }.property('App.Session.objectId', 'owner')
  // }.observes('App.Session.objectId', 'owner')
});

{{#each movie in this}}
  <tr>
    <td>{{movie.year}}</td>
    <td>{{movie.title}}</td>
      {{#if movie.canEdit}}
        <td>
          <span class="clickable glyphicon glyphicon-remove pull-right" {{action "doDeleteMovie" movie}}></span>
          <span class="clickable glyphicon glyphicon-pencil pull-right" {{action "doUpdateMovie" movie}}></span>
        </td>
      {{else}}
        <td>
          <span class="glyphicon glyphicon-remove pull-right text-muted"></span>
          <span class="glyphicon glyphicon-pencil pull-right text-muted"></span>
        </td>
      {{/if}}
    {{/if}}
  </tr>
{{/each}}

0 个答案:

没有答案