2013-01-05 13 views
6

Usando:embedded accesso attributo hasMany dà "TypeError: Impossibile chiamare il metodo 'hasOwnProperty' di non definito"

Simile a this question ('Unable to get hasMany association'), non sono in grado per accedere direttamente a molti record, ma è possibile visualizzarli tramite l'attributo del contenuto del modello.

Per JSON:

{ 
    "ref_book_search":{ 
     "query":"har", 
     "results":[ 
     { 
      "publisher":{ 
       "name":"Pangolin", 
       "created":"2012-09-10T18:38:27.259515", 
       "id":"3d2028e4fb91181e1a6ef821", 
       "is_active":true, 
       "main_url":null, 
       "resource_uri":"/api/v1/ref_publisher/3d2028e4fb91181e1a6ef821" 
      }, 
      "genre":"romcom", 
      "id":"cc671f00fc2711e1e41612313914f821", 
      "resource_uri":"/api/v1/ref_book/cc671f00fc2711e1e41612313914f821", 
      "title":"Harry Placeholder and the Goblet of PBR" 
     }, 
     { 
      "publisher":{ 
       "name":"Hoof & Mouth", 
       "created":"2012-10-10T14:31:27.259515", 
       "id":"3d200e9afb9811e1a27417383914f821", 
       "is_active":true, 
       "main_url":null, 
       "resource_uri":"/api/v1/ref_publisher/3d200e9afb9811e1a27417383914f821" 
      }, 
      "genre":"horror", 
      "id":"cc621f08fc2711e1b81612313914e821", 
      "resource_uri":"/api/v1/ref_book/cc621f08fc2711e1b81612313914e821", 
      "title":"Harvey Weinstein Holiday Cookbook" 
     } 
     ] 
    } 
} 

E app.js (notare le dichiarazioni della mappa, che sono stati la soluzione proposta per la questione venduto):

var App = Ember.Application.create(); 

DS.RESTAdapter.configure("plurals", {"ref_book_search" : "ref_book_search"}); 

App.store = DS.Store.create({ 
    revision: 11, 
    adapter: DS.RESTAdapter.create({ 
    bulkCommits: false, 
    namespace: "api/v1" 
    }) 
}); 

DS.RESTAdapter.map('App.RefBookSearch', { 
    primaryKey: 'query' 
}); 

App.store.adapter.serializer.map('App.RefBookSearch', { 
    results: {embeddded: 'load'} 
}); 

App.store.adapter.serializer.map('App.RefBook', { 
    publisher: {embeddded: 'load'} 
}); 

App.RefPublisher = DS.Model.extend({ 
    name : DS.attr('string'), 
    created : DS.attr('date'), 
    isActive : DS.attr('boolean'), 
    mainUrl : DS.attr('string') 
}); 

App.RefBook = DS.Model.extend({ 
    publisher: DS.belongsTo('App.RefPublisher'), 
    title : DS.attr('string'), 
    genre : DS.attr('string') 
}); 

App.RefBookSearch = DS.Model.extend({ 
    query: DS.attr('string'), 
    results: DS.hasMany('App.RefBook') 
}); 

App.Router.map(function(match) { 
    match('/').to('query'), 
    match('/query/:ref_book_search_id').to('query') 
}); 

App.QueryController = Ember.Controller.extend({ 
    bookSearch: null, 
    results: [] 
}); 

App.QueryRoute = Ember.Route.extend({ 
    setupControllers: function(controller, refBookSearch) { 
    controller.set('bookSearch', refBookSearch) 
    controller.set('results', refBookSearch.get('results').content) 
    } 
}) 

App.initialize(); 

Tutto sembra bene in un primo momento, proprio come altro manifesto:

search = App.RefBookSearch.find('ha') 
search.get('query') 
// => "ha" 
results = search.get('results') 
results.get('firstObject') instanceof App.RefBook 
// => true 

Ma poi:

results.forEach(function(result) { console.log(result.get('title')) }) 
// => TypeError: Cannot call method 'hasOwnProperty' of undefined 

accesso tramite contenuti mostra i dati sono lì:

results.content.forEach(function(result) { console.log(result.title) }) 
// => Harry Placeholder and the Goblet of PBR 
// => Harvey Weinstein Holiday Cookbook 

Ora, se provo accedendo direttamente ancora una volta, ottengo un errore leggermente diverso:

results.forEach(function(result) { console.log(result.get('title')) }) 
// => undefined x 2 

Questo può o non può essere relativo a this bug archiviato alcuni giorni fa.

Mi sento come se avessi provato tutto qui; Spero mi manchi solo qualcosa di semplice. Ogni suggerimento è molto apprezzato. Grazie.

risposta

4

Questo è ciò che alla fine ha funzionato per me. Sembra esserci una certa sensibilità all'ordine delle operazioni, ovvero fare la configurazione e la mappa prima di creare il negozio. Si noti inoltre che adapter.map è una funzione di convenienza che esegue la mappatura sul serializzatore.

App.Adapter = DS.RESTAdapter.extend() 

App.Adapter.configure("plurals", { 
    "ref_book_search" : "ref_book_search", 
    "ref_book" : "ref_book", 
    "ref_publisher" : "ref_publisher" 
}); 

App.Adapter.configure('App.RefBookSearch', { 
    primaryKey: 'query' 
}); 

App.Adapter.map('App.RefBookSearch', { 
    results: {'embedded': 'load'} 
}); 

App.Adapter.map('App.RefBook', { 
    publisher: {'embedded': 'load'} 
}); 

App.store = DS.Store.create({ 
    revision: 11, 
    adapter: App.Adapter.create({ 
    bulkCommits: false, 
    namespace: "api/v1" 
    }) 
}); 
+0

Grazie! Questo risolse il problema esatto che stavo affrontando. –

Problemi correlati