2013-02-15 137 views
5

Emberjs V1.0.0-pre.4設置無法呈現模板與數據setupController

把手1.0.0.rc.2

我建立一個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; 
    } 
}); 

回答

1

不知道這是否會有所幫助,但它看起來像model被忽略或者被定義setupController丟失。我不知道這是否與EmberJS有關。

我的解決方法是直接設置模式:

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

甚至更​​好:

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

可以通過擴展

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

真的,你應該只使用_super實現默認實現。

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