2013-02-18 61 views
3

配合,我有這樣的代碼:骨幹集合創建成功

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

的事情是,我需要得到newCustomer的REST給出的ID,但不知道有什麼方法來等待,直到POST請求回答從服務器。 任何想法?

謝謝!

UPDATE:

我做了它的工作是這樣的:

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

所以,用:

var a = newCustomer.changedAttributes(); 

然後,我可以接觸到的ID,像這樣:

a.attributes.id 

感謝您的幫助!

更新2:

現在的事情是,骨幹不是用新值更新模型的數據從服務器返回。

有什麼想法?

感謝

回答

1

發現的問題! 這是Laravel控制器返回洞Eloquent對象而不是模型的數據。

有了這個代碼,是可以正常使用:

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

非常感謝!

6

您可以在創建選項提供一個success回調:

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

我已經試過這個,但仍然不讓我「得到」身份證。它說它沒有定義。 – Pablo 2013-02-18 20:06:52

+0

您能準確發佈錯誤說的是什麼嗎? – Ben 2013-02-18 20:20:02

+0

這只是'id'屬性是未定義的。 如果我console.log(newCustomer),它不顯示屬性下的任何id,但它顯示在changed.attributes下。 有什麼想法? – Pablo 2013-02-18 20:25:49

0

我發現這個下頁: 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 
    } 
}); 

因此,init newguest回調應該做這份工作,不是嗎?

否則,您應該能夠聽取一次集合與服務器同步後觸發的'重置'事件。

希望它可以幫助