2014-02-20 6 views
1

내 응용 프로그램에서 사용해야하는지 확인하기 위해 노력하고 있습니다. 간단한 응용 프로그램을 테스트하고 있습니다. 모델 간의 관계를 시험해보고 싶었습니다. 이것은 내가 그 모델 정의가 코드입니다 :관계가있는 Emberjs 모델에서 "정의되지 않은 'store'속성을 설정할 수 없습니다.

var App = Ember.Application.create(); 

App.Router.map(function() { 
    this.resource('index', {path: "/"}, function() { 
     this.resource("config", {path: "/config/:config_id"}); 
    }); 
}); 

App.Store = DS.Store.extend(); 

App.Conf = DS.Model.extend({ 
    module : DS.attr(), 
    reports: DS.hasMany('report'), 
    isClean: function() { 
     return !this.get('reports').isAny('isClean', false); 
    }.property('[email protected]') 
}); 


App.Report = DS.Model.extend({ 
    country: DS.attr(), 
    google_account_id: DS.attr(), 
    web_property_id: DS.attr(), 
    custom_source_uid: DS.attr(), 
    isClean: function() { 
     return (
       this.get('country') != '' && 
       this.get('google_account_id') != '' && 
       this.get('web_property_id') != '' && 
       this.get('custom_source_uid') != '' 
       ); 
    }.property('country', 'google_account_id', 'web_property_id', 'custom_source_uid') 
}); 

App.ApplicationAdapter = DS.RESTAdapter.extend({ 
    host: 'http://playground.loc/battle_of_frameworks/json.php' 
}); 

을 ... 그리고 여기에로드되고있는 JSON입니다 : 내가 오류가

:

Error while loading route: TypeError: Cannot set property 'store' of undefined

나는 그 문제를 인터넷 검색하고, 일반적으로 귀하의 모델을 복수형 (예 : App.Reports)으로 명명하는 것에 관한 것입니다. 그래서 여기에 어떤 문제가 있는지 잘 모르겠습니다. 아무도 통찰력을 줄 수 있습니까?

답변

1

코드에 몇 가지 문제가 있습니다.

서버가 Ember Data에서 예상 한 페이로드를 제공하지 않습니다. 백엔드에서 적절한 json 페이로드를 생성 할 수없는 경우 serializer 사용자 정의에 대해 this document을 읽는 것이 좋습니다.

Ember.js는 모든 구성에 대한 관례입니다. 지금, 당신은 그 규칙을 다음되지 않습니다

  • 속성은, it comes for free in Ember 인덱스 경로를 만들 필요가 없습니다

    App.Report = DS.Model.extend({ 
        googleAccountId: DS.attr() //instead of google_account_id 
    }); 
    
  • 을 낙타 표기법 있습니다.

    App.Router.map(function() { this.resource("config", {path: "/config/:config_id"}); });

  • 당신이 당신의 백엔드가 Config/configs/:config_id/config/:config_id에서 제공하지 될 것으로 예상 있는지 위치 : 같은 라우터는 단순히보고해야합니까?

  • config 리소스를 선언했습니다. 국제 대회는 App.Config 모델을 가지고하지 App.Conf

코드 건조, 당신은 또한 계산 된 속성을 이용할 수 있습니다 코드를 정리하기 위해서 :

App.Report = DS.Model.extend({ 
    country: DS.attr(), 
    googleAccountId: DS.attr(), 
    webPropertyId: DS.attr(), 
    customSourceUid: DS.attr(), 
    isClean: Ember.computed.and('country', 'googleAccountId', 'webPropertyId', 'customSourceUid') 
}); 

또한 지불 할 필요를 배열을 기반으로 계산 된 속성을 정의 할 때주의를 기울여야합니다. isCleanConfig 인 경우 isCleanReport이지만 계산 된 속성은 Report 연관의 요소 만 관찰합니다. 그것을 쓰는 올바른 방법은 다음과 같습니다 :

App.Config = DS.Model.extend({ 
    module : DS.attr(), 
    reports: DS.hasMany('report'), 
    isClean: function() { 
    return !this.get('reports').isAny('isClean', false); 
    }.property('[email protected]') //make sure to invalidate your computed property when `isClean` changes 
}); 

나는 이것이 도움이되기를 바랍니다.

+0

고마워. 내가 제안한 모든 변경 사항을 추가하고 json 형식을 변경했다. 건배! – aledujke

관련 문제