2016-03-01 43 views
0

我router.js:Ember JS:如何在父組件中設置屬性?

Router.map(function() { 
    this.route('dashboard', { path: '/' }); 
    this.route('list', { path: '/list/:list_id' }, function() { 
     this.route('prospect', { path: 'prospect/:prospect_id', resetNamespace: true }); 
    }); 
}); 

list.hbs:

<div class="content-wrapper"> 
    <div class="content"> 
     {{prospect-list name=model.name prospects=model.prospects openProspect="openProspect"}} 
    </div> 
</div> 

{{outlet}} 

前景-list.hbs:

{{#each prospects as |prospect|}} 
    {{prospect-item prospect=prospect openedId=openedProspectId openProspect="openProspect"}} 
{{/each}} 

前景,item.hbs

<td>{{prospect.firstName}}</td> 
<td>{{prospect.list.name}}</td> 

組件/前景列表。 JS

import Ember from 'ember'; 

export default Ember.Component.extend({ 

    openedProspectId: 0, 

    actions: { 
     openProspect(prospect) { 
      this.set('openedProspectId', prospect.get('id')); 
      this.sendAction('openProspect', prospect); 
     }    
    }  
}); 

組件/前景-list.js

import Ember from 'ember'; 

export default Ember.Component.extend({ 
    tagName: 'tr', 
    classNameBindings: ['isOpen:success'], 

    isOpen: Ember.computed('openedId', function() { 
     return this.get('openedId') == this.get('prospect').id; 
    }), 

    click: function(ev) { 
     var target = $(ev.target); 
     if(!target.is('input')) 
     {  
      this.sendAction('openProspect', this.get('prospect')); 
     } 
    } 

}); 

一切工作很好,當我在瀏覽器中啓動應用程序與http://localhost:4200,但是當我從http://localhost:4200/list/27/prospect/88當前加載的前景(ID爲88)開始不在預期列表中突出顯示,因爲初始打開的縱向標記集爲0.

在這種情況下,我如何設置opensProspectId?

我能得到像路由/ prospect.js這些ID:

import Ember from 'ember'; 

import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin'; 

export default Ember.Route.extend(AuthenticatedRouteMixin, { 

    model(params) { 
     return this.store.findRecord('prospect', params.prospect_id); 
    }, 

    afterModel(params) { 
     console.log(params.id); 
    } 
}); 

但我該如何將它傳遞給openedProspectId?還是應該以另一種方式構建我的應用程序?

回答

1

有幾件事情可以在這裏重做。我將首先使用鏈接幫助程序,而不是在單擊潛在客戶時發送操作。這將給你一個統一的起點(路線),並允許用戶在他們決定的時候在新窗口中打開潛在客戶。

該路線自然會在控制器上設置屬性model。您可以將其傳遞到activeProspect的個人潛在客戶項目組件中。然後在該組件內比較prospect.id == activeProspect.id以確定該行是否應該突出顯示。

我有一條單獨的路線來突出潛在客戶,但我不瞭解您的業務需求似乎很奇怪。你可以考慮使用queryParams來產生一個像這樣的網址list/27?prospect=88,並保留前景的'全景'路線。

+0

謝謝。我使用鏈接到幫助器,這解決了我的問題,因爲它會在應用程序加載時設置活動類。 – Dmytro

相關問題