2012-09-21 2 views
3

내가 엠버 데이터에 대한 로컬 스토리지의 adaper를 압연하고 던져 나는 찾기 기능, 즉 실행하면엠버 데이터 로컬 스토리지 어댑터 : 기능을 찾을 오류

App.store.find(App.Person, 0); 

나는이 오류가 무엇입니까 :

Uncaught Error: assertion failed: A data hash was loaded for a model of type App.Person but no primary key 'undefined' was provided. 

좀 더 일반적인 수준에서 나는 '영구적 인 계층'(이 경우 localStorage)과 Ember 저장소 사이의 관계에 대해 약간 혼란 스럽습니다. 상점에 무언가를 적재한다는 것은 무엇을 의미합니까? 그것은 DS.Person 모델의 인스턴스가 데이터로 생성된다는 것을 의미합니까?

또한 'createRecord'메서드에서 'App.store.didCreateRecord (model, data)'행을 주석 처리 했으므로이 코드도 작동하지 않습니다. 레코드가 localStorage에로드 된 후 store.didCreateRecord를 호출하지 않으면 어떻게됩니까?

세부 사항 :

DS.LocalStorageAdapter = DS.Adapter.extend({ 

    get: Ember.get, 
    set: Ember.set, 

    createRecord: function(store, modelType, model){ 
     var records, index, data; 
     // get existing records of this model 
     records = this.localStorage.get(modelType); 
     index = records.length; 
     data = this.get(model, 'data'); 
     // set storageID of data 
     data.set('storageID', index); 

     // add data to existing records 
     records[index] = data; 
      // encode records 
     records = JSON.stringify(records); 
      // store records in localStorage 
     this.localStorage.set(modelType, records); 
     // App.store.didCreateRecord(model, data); 

    }, 



    find: function(store, modelType, id) { 
     var records, model; 
     records = this.localStorage.get(modelType); 
     model = records[id]; 
     App.store.load(modelType, model); 
    }, 

    localStorage: { 
     set: function(modelType, value){ 
      localStorage.setItem(modelType, value); 
     }, 

     get: function(modelType){ 
      var record = localStorage.getItem(modelType); 
      record = JSON.parse(record) || []; 
      return record; 
     } 

    } 
}); 



//application model 
App.Person = DS.Model.extend({ 
    name: DS.attr('string', {key: 'css_name'}), 
    storageID: DS.attr('number', {defaultValue: 0, key: 'storageID'}), 

}); 

//create a couple of records 
App.store.createRecord(App.Person, { ... }); 
App.store.createRecord(App.Person, { ... }); 

//try and find one record 
App.store.find(App.Person, 0); 

error thrown: 
Uncaught Error: assertion failed: A data hash was loaded for a model of type App.Person but no primary key 'undefined' was provided. ember-latest.js:51 
Ember.assert ember-latest.js:51 
(anonymous function) ember-latest.js:131 
DS.Store.Ember.Object.extend.load ember-data.js:1530 
DS.LocalStorageAdapter.DS.Adapter.extend.find bookApp_1.js:111 
DS.Store.Ember.Object.extend.findByClientId ember-data.js:1174 
DS.Store.Ember.Object.extend.find ember-data.js:1140 
findRecord webApplication.js:210 
onclick 

는 또 다른 문제는 조금 전에 게시에이 비슷합니다 유의하시기 바랍니다 : Ember Data: A data hash was loaded ... but no primary key 'undefined' was provided

대답 정말로 무슨 일이 일어나고 있는지 설명 할 것 같습니다.

+0

Ember의 버전은 무엇입니까? – lesyk

답변

0

은 엠버 1.0 PRE2

나를 위해
App.store = DS.Store.create({ 
    revision: 11, 
    adapter: 'DS.FixtureAdapter' 
}); 

App.User = DS.Model.extend({ 
    email: DS.attr('string'), 
    password: DS.attr('string') 
}); 

App.User.FIXTURES = [ 
    { 
    "id": "1", 
    "email": "[email protected]", 
    "password": "password" 
    } 
]; 

App.User.find(1).get('email') 

작품에 대한로드 고정 장치 데이터의 예를 제공 할 수 있습니다.

+0

오오 그래 Ember 데이터가 너무 많이 변경 되었기 때문에이 질문을 올렸습니다. 내 질문이 여전히 관련성이 있는지는 모르겠지만 대답과 예제를 통해 고맙습니다! :) – chibro2

관련 문제