Building a core network creates success

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, I need to get the newCustomer RESTFul id, but I don't know which method to wait until a message response is sent to the server. Any ideas?

Thank!

UPDATE:

I did it like this:

            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 the id, for example:

a.attributes.id

      

Thanks for the help!

UPDATE 2:

Now the point is that the backbone does not update the model data with the new values ​​returned from the server.

Any idea?

thank

+3


source to share


3 answers


Found a question! It was that the Laravel controller was returning a hole Eloquent object, not just model data.

Works fine with this code:



        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
                });
            }
        });

      

Thank you so much!

+1


source


You can provide a callback success

in the build parameters:



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
        });
    }
});

      

+6


source


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, right?

Otherwise, you will be able to listen for the reset event, which is fired after the collection is synchronized with the server.

Hope it helps

0


source







All Articles