【问题标题】:Firebase: Update Item Timestamp on $saveFirebase:在 $save 上更新项目时间戳
【发布时间】:2015-04-23 04:43:19
【问题描述】:

我正在研究如何更新我的 Firebase 项目上的 updatedAt 时间戳。

更新的thing.namething.description 已更新到对象,但我不知道如何更新时间戳,即使我在editedThing 对象中传递它。

这是我当前的代码:

HTML

<h1>You are editing </h1>
<section class="unit">
  <form ng-submit="updateThing(selectedThing)">
    <input type="text" name="name" ng-model="selectedThing.name" value=""><br>
    <input type="text" name="name" ng-model="selectedThing.description" value="">
    <button type="submit">Submit</button>
  </form>
</section>

JS

var ref = new Firebase(FBURL);
var thingsRef = ref.child('things');
var things = $firebaseArray(thingsRef);
$scope.things = things;

// Add thing

$scope.addThing = function(thing) {
  var d = new Date();
  var newThing = {
    name: thing.name,
    description: thing.description,
    createdAt: d.toISOString(),
    updatedAt: d.toISOString()
  };

  things.$add(newThing);
};

// Update thing

var serviceId = $routeParams.serviceId;

if(serviceId) {
  $scope.selectedThing = getThing(serviceId);
}

function getThing(serviceId) {
  return $firebaseObject(ref.child('things').child(serviceId));
}

$scope.updateThing = function(thing) {
  var d = new Date();
  var editedThing = {
    name: thing.name,
    description: thing.description,
    updatedAt: d.toISOString()
  }

  $scope.selectedThing.$save(editedThing);
};

如果我更新 namedescription,则这些值会在 Firebase 中更新。但更新时不会生成新的时间戳,也不会推送到 Firebase。

知道我做错了什么吗?任何帮助表示赞赏。提前致谢!

【问题讨论】:

  • 由于您的$scope.selectedThing 指的是$firebaseObject,因此您只需修改它($scope.updatedAt = d.toISOString())并在其上调用$save。您可能应该考虑使用Firebase.ServerValue.TIMESTAMP btw:firebase.com/docs/web/api/servervalue/timestamp.html
  • 使用基于 UNIX 的时间戳有什么好处吗?
  • 重要的区别不在于格式,而在于可靠性。如果您有两个客户端并且它们的时钟完全不同步,那么您的客户端生成的时间戳将是一种不可靠的机制,例如用于订购数据。当您使用Firebase.ServerValue.TIMESTAMP 时,时间戳将始终在服务器上生成。

标签: angularjs firebase angularfire


【解决方案1】:

利用内置的$extend 功能,您可以在保存期间添加时间戳而无需大惊小怪。

app.factory('SyncWithTimestamp', function($firebaseObject) {
   return $firebaseObject.$extend({
      toJSON: function() {
         return {
            name: this.name,
            description: this.description,
            createdAt: this.createdAt,
            // when data is returned to the server, set the updatedAt
            // to a new, server-generated timestamp (to avoid client discrepancies)
            updatedAt: Firebase.ServerValue.TIMESTAMP
         }
      }
   });
});

现在你只需要正常使用扩展工厂:

app.controller(..., function(SyncWithTimestamp) {

   $scope.selectedThing = new SyncWithTimestamp(thingsRef.child(serviceId));

});

updateThing() 方法是多余的,可能会产生错误。无需从同步对象中复制数据,然后尝试将其保存回同步对象(无论如何,这不是通过将 args 传递给 $save() 来完成的)。你可以用这个:

<form ng-submit="selectedThing.$save()">

the guide 中涵盖了所有这些以及更多 AngularFire 的基础知识,并附有示例片段。详细了解它会为您省去一些痛苦和痛苦。

【讨论】:

    【解决方案2】:

    $save() 不是这样工作的,它不带任何参数,你必须改变原来的 $scope 然后调用 $save()。

    你可以试试:

    $scope.updateThing = function(thing) {
    var d = new Date();
      $scope.selectedThing.name = thing.name;
      $scope.selectedThing.description = thing.description;
      $scope.selectedThing.date = d.toISOString()
    
      $scope.selectedThing.$save();
    };
    

    【讨论】:

    • 似乎无法让它发挥作用。收到此错误:t.selectedService.$save is not a function.
    • 我编辑了代码,我认为你可以通过 html ng-submit="selectedThing.$save()" 实现这一点
    猜你喜欢
    • 2020-07-18
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2011-02-20
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多