嗨,我想知道是否有一种方法可以使日期成为父值?目前正在生成一个随机值。

  $(function(){

// CREATE A REFERENCE TO FIREBASE
 var dateRef = new Firebase('https://shiftsapp.firebaseio.com/roster');

 // REGISTER DOM ELEMENTS
 var date1Field = $('#date1Input');
 var emp1put1Field = $('#emp1Input1');
 var emp2put1Field = $('#emp2Input1');
 var emp3put1Field = $('#emp3Input1');
 var emp4put1Field = $('#emp4Input1');
 var emp5put1Field = $('#emp5Input1');

 // LISTEN FOR KEYPRESS EVENT
 emp5put1Field.keypress(function (e) {
if (e.keyCode == 13) {
  //FIELD VALUES
  var date = date1Field.val();
  var emp1put1 = emp1put1Field.val();
  var emp2put1 = emp2put1Field.val();
  var emp3put1 = emp3put1Field.val();
  var emp4put1 = emp4put1Field.val();
  var emp5put1 = emp5put1Field.val();

  //SAVE DATA TO FIREBASE AND EMPTY FIELD
  dateRef.push({date:date, emp1:emp1put1, emp2:emp2put1, emp3:emp3put1, emp4:emp4put1, emp5:emp5put1});
  emp5put1Field.val('');
   }
 });


});

这是当前保存为

javascript - 如何使Date为父级值?-LMLPHP

我希望它输出如下:

     date:18/01
     --- emp1 = 9am
     --- emp2 = 10am
     --- emp3 = 11am
     --- emp4 = 12am
     --- emp5 = 2pm

最佳答案

您需要使用update()push()创建一个唯一的ID作为数据的父级,然后将其保存在您指定的树下。尝试以下方法:

//SAVE DATA TO FIREBASE AND EMPTY FIELD
var obj = {};
obj[date] = {
    emp1:emp1put1,
    emp2:emp2put1,
    emp3:emp3put1,
    emp4:emp4put1,
    emp5:emp5put1
}

dateRef.set(obj);
emp5put1Field.val('');


* set()将覆盖数据,而update()仅将覆盖相同键的数据(如果您尝试插入同一日期的另一个对象,它将被覆盖,否则将被“推送”到roster/上)

Firebase具有出色的文档和专用于保存数据的页面:

Saving Data

07-24 09:44