3
votes

Mates, i have this code:

             var newCustomer = customers.create({
                'id_pais' : usrinfo.country,
                'nombre' : usrinfo.name,
                'apellido' : usrinfo.lastname,
                'pasaporte' : usrinfo.passport,
                'mail' : usrinfo.mail,
                'birth' : usrinfo.birth
            });
            console.log(newCustomer.get('id'));
            // Create Guest
            var cama = beds.get(usrinfo.dorm);
            var newGuest = guests.create({
                'id_room' : cama.get('id_room'),
                'id_bed' : usrinfo.dorm,
                'id_customer' : newCustomer.get('id'),
                'inDate' : usrinfo.inDate,
                'outDate' : usrinfo.outDate,
                'notas' : usrinfo.notes
            });

The thing is, that I need to get newCustomer's RESTFul given id, but don't know any method to wait until the post request is answered from the server. Any ideas?

Thanks!

UPDATE:

I made it work this way:

            var newCustomer;
            newCustomer = customers.create({
                'id_pais' : usrinfo.country,
                'nombre' : usrinfo.name,
                'apellido' : usrinfo.lastname,
                'pasaporte' : usrinfo.passport,
                'mail' : usrinfo.mail,
                'birth' : usrinfo.birth
            }, {
                success: function(response){
                    var a = newCustomer.changedAttributes();
                    var cama = beds.get(usrinfo.dorm);
                    var newGuest = guests.create({
                        'id_room' : cama.get('id_room'),
                        'id_bed' : usrinfo.dorm,
                        'id_customer' : a.attributes.id,
                        'inDate' : usrinfo.inDate,
                        'outDate' : usrinfo.outDate,
                        'notas' : usrinfo.notes
                    });
                }
            });

So, with:

var a = newCustomer.changedAttributes();

Then I can access to the id, like this:

a.attributes.id

Thanks for the help!

UPDATE 2:

Now the thing is, that backbone's not updating the model's data with the new values returned from the server.

Any idea?

Thanks

3

3 Answers

6
votes

You can supply a success callback in the create options:

var newCustomer;
newCustomer = customers.create({
    'id_pais' : usrinfo.country,
    'nombre' : usrinfo.name,
    'apellido' : usrinfo.lastname,
    'pasaporte' : usrinfo.passport,
    'mail' : usrinfo.mail,
    'birth' : usrinfo.birth
}, {
    success: function() {
        console.log(newCustomer.get('id'));
        // Create Guest
        var cama = beds.get(usrinfo.dorm);
        var newGuest = guests.create({
            'id_room' : cama.get('id_room'),
            'id_bed' : usrinfo.dorm,
            'id_customer' : newCustomer.get('id'),
            'inDate' : usrinfo.inDate,
            'outDate' : usrinfo.outDate,
            'notas' : usrinfo.notes
        });
    }
});
1
votes

Found the issue! It was that the Laravel controller was returning the hole Eloquent object instead of just the model's data.

With this code, is working perfectly:

        var newCustomer;
        newCustomer = customers.create({
            'id_pais' : usrinfo.country,
            'nombre' : usrinfo.name,
            'apellido' : usrinfo.lastname,
            'pasaporte' : usrinfo.passport,
            'mail' : usrinfo.mail,
            'birth' : usrinfo.birth
        }, {
            success: function(response){
                var a = newCustomer.changedAttributes();
                var cama = beds.get(usrinfo.dorm);
                var newGuest = guests.create({
                    'id_room' : cama.get('id_room'),
                    'id_bed' : usrinfo.dorm,
                    'id_customer' : a.attributes.id,
                    'inDate' : usrinfo.inDate,
                    'outDate' : usrinfo.outDate,
                    'notas' : usrinfo.notes
                });
            }
        });

Thanks a lot!

0
votes

I found this on the following page : http://lab.devaddiction.com/backbone-js-tutorial-synchronization-and-persistence/

user.save({}, {              // generate POST /users - content: {name: 'John'}
     success: function() {
        // Assuming that the server returned the object {"id": 1}
        alert(user.id);  // show 1
     }
 }); 

So init newguest in the callback should do the job, shouldn't it ?

Otherwise, you should be able to listen to the 'reset' event which is triggered once a collection is synced with server.

Hope it helps