0

I have a User model:

var UserModel = Backbone.Model.extend({
    defaults: {
        handle: '',
        email: '',
        uuid: '',
        userpic: '',
        tokenlogin: ''
    }
});

I also have a collection called UserSignIn, although I'm not sure why:

var UserSignIn = Backbone.Collection.extend({ model: UserModel });

And inside of my SignInView view, I have the following function...

signIn: function(e) {
    e.preventDefault();

    this.collection.fetch({
        type: 'POST',
        url: 'http://localhost/app/api/User.php',
        dataType: "json",
        data: $.param({
            req: "REQUSERSIGNIN",
            platform: "WEB",
            useremail: $('#userSignIn #userEmail').val(),
            userpass: $('#userSignIn #userPassword').val()
        }),
        success: function(data) {

            // In here, I'd like to create an
            // instance of the model which I'd
            // like to pass around my app.

            var user = new UserModel({
                handle: data.HANDLE,
                email: data.EMAIL,
                uuid: data.UUIDUSER,
                userpic: data.USERPIC,
                tokenlogin: data.TOKENLOGIN
            });
        }
    });
}

As you can see, all I am trying to do is create an instance of a User on success of the BackBone.fetch() function.

I'd like to understand how to then pass around this new "user" UserModel() instance around my app. When I try to console.log(user) I get a "ReferenceError: user is not defined" when clearly I just created it in the success callback of the fetch function.

Can someone explain to me why?

4

2 に答える 2

1

バックボーンで正しい方法に従いたい場合は、コレクションに挿入する必要があります。私はあなたがこれを行うことができると思います:

ビューの初期化にこれを挿入します:

initialize: function(){
   //..... your code in initialize
   this.userModel = null;
   this.collection = new UserCollection();
},


signIn: function(e) {
    e.preventDefault();
    var here = this;
    this.collection.fetch({
        type: 'POST',
        url: 'http://localhost/app/api/User.php',
        dataType: "json",
        data: $.param({
            req: "REQUSERSIGNIN",
            platform: "WEB",
            useremail: $('#userSignIn #userEmail').val(),
            userpass: $('#userSignIn #userPassword').val()
        }),
        success: function(data) {
             var user = {handle: data.HANDLE,email: data.EMAIL,uuid: data.UUIDUSER,userpic: data.USERPIC,tokenlogin: data.TOKENLOGIN};
            here.userModel = new UserModel(user);
            here.collection.addUser(here.userModel);
        }
    });
}

UserCollection は次のようにする必要があります。

var UserCollection = Backbone.Collection.extend({
    model: UserModel,
    initialize:function(){
        console.log('Initialized User collection');
    },
        addUser: function(users, options) {
            return this.add(users, options);
        }
});

コレクションの各要素をコンソールするには、これを試すことができます (成功関数内でこのコードを実行する場合は、here代わりに を使用しますthis):

this.collection.each(function(user, index) {
    console.log(user);
    //if you want to set a value of your model:
    user.set('email', 'yournewemail@email.it');
    //if you want to get some value
    user.get('email');                          
});
于 2013-09-18T18:24:53.040 に答える
0

変数はSignInView ビューuserの関数内にのみスコープが設定されているため、グローバル スコープから変数を検索することはできません。関数内でユーザーを作成した直後に配置して、作成されたことを確認できます。これにより、ローカル変数が検出されるためです。successconsole.log(user)userconsole.log(user)successuser

アプリからアクセスするにはvar user;、fetch 関数の外側で宣言し、fetch関数内で設定するだけです。

于 2013-09-18T18:24:44.097 に答える