0

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 !

4

3 回答 3

1

由于您的 URL 方案不是默认的“/object/id”,因此您应该更新 URL:

window.User = Backbone.Model.extend({
    defaults : {
        id : "1",
        username : "anonymous",
        facebook_id : "1235486421",
        facebook_token : "AEZH93FZFEFSFSFS4545154sfsfSF"
    }, events {
        "change" : "updateUrl"
    },
    initialize : function() {
        console.log('User Constructor...');
        this.updateUrl();
    },
    updateUrl : function() {
        this.url = "http://localhost:3000/user/" +
                   this.get('username') +
                   '/'+this.get('facebook_id') + 
                   '/'+this.get('facebook_token');
    }
});
于 2012-10-11T19:04:51.957 回答
0

我浪费了差不多一个小时。您需要注意的是模型在获取成功方法之后得到更新,

 CurrentUser.fetch({
                success: function(_model){
                  console.log(_model);
 }
于 2012-12-06T22:18:13.617 回答
0

Backbone 根据设置为模型的 id 属性的任何内容进行获取。当您实例化新用户时,id 默认设置为 1。当您调用 fetch 时,Backbone 会尝试根据 urlRoot 进行获取(因为它不是集合的一部分),而不是 URL:http:/localhost:3000/user/1

http://backbonejs.org/#Model-url

http://backbonejs.org/#Model-urlRoot

如果您希望基于某些非 id 属性进行获取,您可能需要考虑覆盖 Backbone 的获取。

于 2012-10-11T18:52:05.467 回答