I have an issue with a model, i instanciate it up with some default values, i am using the set()
method to change it, and when i use the fetch()
méthod, i get the defaults values instead the new ones, i tried to log myModel.toJSON()
in the console and i see the new values, i launch myModel.fetch()
i get the defaults values on the server side, crazy no ?
Here is the model prototype:
window.User = Backbone.Model.extend({
defaults : {
id : "1",
username : "anonymous",
facebook_id : "1235486421",
facebook_token : "AEZH93FZFEFSFSFS4545154sfsfSF"
},
urlRoot: 'http:/localhost:3000/user',
initialize : function() {
console.log('User Constructor...');
this.url = "http://localhost:3000/user/"+this.get('username')+'/'+this.get('facebook_id')+'/'+this.get('facebook_token');
}
});
Here is the chunck of code where i use fetch()
:
fetch_user: function(){
var CurrentUser = new User();
FB.login(function(response) {
if (response.authResponse) {
var access_token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + ', id: '+response.id+', token: '+access_token);
CurrentUser.set('username', response.name);
CurrentUser.set('facebook_id', response.id);
CurrentUser.set('facebook_token', access_token);
console.log('Current user is: ' + CurrentUser.toJSON());
CurrentUser.fetch({
success: function(_model){
console.log('User fetched successfully');
},
error: function(_model, error){
console.log('User fetched unsuccessfully');
}
});
});
}
else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
},
Thanks !