Ho un modello base backbone, l'attributo urlRoot
è impostato e il target corrispondente sul lato server restituisce un output JSON corretto (sia la stringa JSON che l'intestazione application/json
).Impossibile recuperare Backbone.js attributi in realtà
io chiamo un prendere come questo:
var athlete = new Athlete({ id: 1 });
athlete.fetch();
a questo punto, se posso aggiungere un
console.log(athlete);
posso vedere il modello, e ispezionare in Firebug posso aprire gli attributi oggetto e vedere tutti i valori restituiti dal server.
Ma se faccio un:
console.log(athlete.get('name'));
ottengo undefined
(compare il nome sotto gli attributi nella ispezione DOM che ho citato sopra)
anche facendo un:
console.log(athlete.attributes);
restituisce un oggetto contenente solo {id: 1}
, che è l'argomento che ho passato durante la creazione del modello.
Se creo il modello come questo:
var athlete = new Athlete(<JSON string copypasted from the server response>);
poi tutto funziona bene, il metodo .get()
restituisce tutto quello che chiedo, e athlete.attributes
mostra tutti i valori.
Cosa sto sbagliando?
Naturalmente ora mi rendo conto di quanto fosse ovvio, grazie per aver segnalato :) –