2016-06-02 4 views
1

routes/application.js 경로에서 사이트 전체에 사용되는 일부 ember 데이터를로드 중입니다. 그 데이터는 어디서나 액세스 할 수 있습니다. 특히 데이터는 templates/application.hbs 템플릿의 구성 요소로 전달되며은 다른 여러 장소에서 액세스합니다. 사용자가 인증되지 않은 경우ember-simple-auth 인증 후 application.js에로드되는 Ember 데이터 모델이로드되지 않습니다.

것은, 내가 쿼리 할 수 ​​없습니다 :이 올바른 접근 여부는 (! 내가 그에 대한 피드백을 환영합니다) 여부

, 이것은 다음과 같은 경우를 제외하고, 나를 위해 작동 데이터를 볼 수있는 권한이 없기 때문에 이것은 내가 가지고있는 코드입니다 : 나는 그들이 인증 후 데이터로드를하고 싶습니다

// routes/application.js 
import Ember from 'ember'; 

export default Ember.Route.extend(ApplicationRouteMixin, { 
    model() { 
    if (this.get('session.isAuthenticated')) { 
     return Ember.RSVP.hash({ 
     clients: this.store.findAll('client', {include: 'projects'}), 
     resources: this.store.findAll('resource') 
     }); 
    } 
    } 
}); 

,하지만 난 모델 재로드를 만드는 방법을 알고하지 않습니다. 방금 sessionAuthenticated 후크에 데이터로드가있는 경우 다음과 같이 입력하십시오.

// routes/application.js 
sessionAuthenticated() { 
    this.controller.set('clients', this.store.findAll('client', {include: 'projects'})); 
    this._super(...arguments); 
} 

작동하지 않습니다. store은 데이터로 채워지지만이 데이터에 의존하는 구성 요소는 데이터를 보지 못합니다. 또한, 다음에 전환하는 경로는 데이터에 따라 다르지만 동시성으로 인해 시간이 경과하지 않습니다.

이 작업을 수행하는 간단한 방법이 있어야하지만, 나는 그것이 무엇인지에 대해 당혹 스럽다. 옵저버? session.isAuthenticatedtrue 일 때 routes/application.jsmodel() 메서드를 다시 실행하여 (약속이 반환 될 때까지 대기) 이제?

엠버 : 2.5.x의, 엠버 데이터 : 2.5.x의

+1

, 문제는 응용 프로그램 경로에이 일을하는 것입니다. ** 서비스 **에서이 모델을 제공하는 것을 고려 했습니까? 클라이언트/리소스에 대한 액세스가 필요한 경로에 서비스를 주입하십시오. –

+1

그게 올바른 접근 방식 같아. 다음 주까지는 시도 할 수 없지만 여기서 다시보고하겠습니다! – pauldoerwald

+0

이 방법은 대개 성공적이었습니다. 나는 그 나머지 부분에 대답하기를 희망하는 [follow-on question] (http://stackoverflow.com/questions/37756214/accessing-a-services-promise-from-a-route)을 올렸다. 그 때까지, 나는이 문제에 대해 내가 생각해 낸 것에 대답 할 것이다. – pauldoerwald

답변

1

다음과 같이 내가 서비스를 구현 :

// services/lime-core.js 
import Ember from 'ember'; 

export default Ember.Service.extend({ 
    store: Ember.inject.service(), 
    resources: null, 
    clients: null, 

    init() { 
    this.set('resources', []); 
    this.set('clients', []); 

    this.get('store').findAll('resource').then(resources => { 
     this.set('resources', resources); 
    }); 
    this.get('store').findAll('client', {include: 'projects'}).then(clients => { 
     this.set('clients', clients); 
    }); 
    } 
}); 

가 I가 다음 수 있었다 템플릿이 삽입 된 경우 limeCore 서비스에 액세스하십시오.

// components/foo/component.js 
export Ember.Component.extend({ 
    limeCore: Ember.Service.inject(), 
    ... 
} 

내 템플릿 :

당신이 이미 알고 있듯이
// components/foo/template.hbs 
<ul> 
    {{#each limeCore.resources as |resource|}} 
    <li>{{resource.name}}</li> 
    {{/each}} 
</ul> 
0

이 시도 :

// routes/application.js 
import Ember from 'ember'; 

export default Ember.Route.extend(ApplicationRouteMixin, { 
    model() { 
    if (this.get('session.isAuthenticated')) { 
     return Ember.RSVP.hash({ 
     clients: this.store.findAll('client', {include: 'projects'}), 
     resources: this.store.findAll('resource') 
     }); 
    } 
    return {}; 
    }, 
    sessionAuthenticated() { 
    this.store.findAll('client', {include: 'projects'}).then(clients => { 
     this.controller.set('model.clients', clients); 
    }); 

    this._super(...arguments); 
    } 
}); 
+0

이것은 실제로 작동하지 않았습니다. 적어도이 접근 방식을 사용하면 쿼리를 동 기적으로 수행해야한다고 생각합니다. 위에 설명 된 서비스 접근 방식을 시도해 보겠습니다. – pauldoerwald

관련 문제