无法使用setupController中的数据集渲染模板

时间:2013-02-15 23:51:31

标签: ember.js

Emberjs v1.0.0-pre.4

Handlebars 1.0.0.rc.2

我正在ember中建立一个facebook好友列表管理器。当我导航到list/1/members时,没有出现MembersRoute填充MembersController的数据。

MembersRoute将呈现朋友模板,该模板通常显示我们所有的朋友,但在此方案中仅显示所选列表的成员。

应用程序的路由器

App.Router.map(function () {
    this.route('instructions');
    this.route('friends');
    this.route('members', {path: '/list/:list_id/members'});
});

路线

App.MembersRoute = Ember.Route.extend({
    model: function () {
        var list = this.controllerFor('application').get('selectedList'),
            friends = list && list.get('members') || [];
        return friends;
    },
    setupController: function (controller, model) {
        var list = this.controllerFor('application').get('selectedList');
        controller.set('title', list.get('title'));
        controller.set('list', list);
        controller.set('content', model);
    },
    renderTemplate: function () {
        this.render('friends');
    }
});

朋友模板

<script type='text/x-handlebars' data-template-name='friends'>
  <h2>{{title}}</h2>
  <ul id='friends'>
    {{#each friend in controller}}
      <a href='#' {{action 'select' friend}}>
        <li {{bindAttr class='friend.isSelected:selected'}}>
            <img {{bindAttr src='friend.imageUrl'}}/>
            <p>{{friend.name}}</p>
        </li>
      </a>
    {{else}}
      <h3>There are no friends in this list.</h3>
    {{/each}}
  </ul>
</script>

申请模板

<script type='text/x-handlebars' data-template-name='application'>
      <div class='row'>
          <div class='span4'>
              {{render 'lists'}}
          </div>
          <div class='span8>
              {{outlet}}
          </div>
      </div>
</script>

最后,数据存储和模型

App.Store = DS.Store.extend({
  revision: 11,
  adapter: 'DS.FixtureAdapter'
});

App.List = DS.Model.extend({
    members: DS.hasMany('App.Friend'),
    name: DS.attr('string'),
    isSelected: DS.attr('boolean'),
    num_of_members: function () {
        var num = this.get('members').toArray().length,
        str = num + ' Member';
        return num === 1 ? str : str + 's';
    }.property('members'),
    title: function () {
        return 'Friends in ' + this.get('name');
    }.property('name'),
    toggleSelected: function () {
        this.set('isSelected', !this.get('isSelected'));
        return this;
    }
});

App.Friend = DS.Model.extend({
    lists: DS.hasMany('App.List'),
    name: DS.attr('string'),
    imageUrl: DS.attr('string'),
    isSelected: DS.attr('boolean'),
    toggleSelected: function () {
        this.set('isSelected', !this.get('isSelected'));
        return this;
    }
});

2 个答案:

答案 0 :(得分:2)

实际上你应该只使用_super来实现默认实现。

setupController: function(controller, model) {
  this._super(controller, model)
  controller.set('something', 'some value');
}

答案 1 :(得分:1)

不确定这是否有帮助,但在model定义时,setupController似乎会被忽略或丢失。我不知道这是否是EmberJS的错误。

我的解决方法是直接设置模型:

model: function() {
   return this.modelFor('todos');
} 

可以延伸:

setupController: function(controller) {
  controller.set('something', 'some value');
  controller.set('model', this.modelFor('todos'));    // this was originally in the model function
}

甚至更好:

setupController: function(controller, model) {
  controller.set('something', 'some value');
  controller.set('model', model);    // this was originally in the model function
}