0

I am using angularJS 1.3 with angularfire and firebase.

I did create some factories that encapsulate the firebase object, and it works very well. The 3 way data binding is really cool.

I am planning to build a large application using firebase for data persistance, but I am facing a massive issue :

In my business model, I want some models/collections to support 3-way data binding, but I want other collections not to be updated live.

Some clients visiting the resulting website might be scared when they see the website's content being updated live - and I don't want that. Imagine you are reading an article , somehow long and complexe, and you put your attention into a paragraph which you have issues understanding, and suddenly, the paragraph disappears and is replaced by a new one. Now imagine that you have no idea about web development. You might start to think to consult some professionnal about your mental health, or think your computer is hacked. Who knows !

So I would like to disable the 3 way data binding for this article, and avoid this border-line client phone calls. The only solution I found would be not to use fireangular, and use the beyond angularfire documentation section instead, and remove the following lines :

ref.on("value", function(snapshot) {
   $timeout(function() {
     $scope.data = snapshot.val();
   });
});  

This would fix the issue, but then in my admin interface, I would have to implement a second Factory so admin users can access the 3 way data binding for articles - Which is not very DRY.

In my artitcle controller, I would have to write something like this :

$scope.loadArticle = function(){
   if(UserService.type === 'admin'){
       AdminArticleFactory.get(id);
   }
   else{
       ArticleFactory.get(id);
   }
}

Is there any other way of setting up firebase to disable the pull updates?

Ant
  • 1,812
  • 5
  • 22
  • 39
  • This doesn't make any sense. It says: "I want to synchronize but not synchronize." If you don't want other clients to see the data, then don't write to the path they are listening on. Write to a different path. It sounds like you're trying to write an app without first reading the docs and understanding the tools you're utilizing, and this would be a recipe for pain and wasted time. – Kato May 16 '15 at 16:05
  • @Kato Thanks for your 'constructive' comment. Please clarify your comment on your suggestion of writing to a different path, I want to synchronize for a type of users, and not synchrornize for guests. Replace "but" with "and", and you get it right. "I want to synchronize AND not synchronize" – Ant May 16 '15 at 16:32

1 Answers1

2

Yeah, that is what I also ran into. Sometimes you will repeat yourself with Firebase. Also when storing data. I would recommend just making a function to save the data that you don't want to be updated instantly.

<button ng-click="saveChanges">save</button>

In controller

$scope.saveChanges = function () {
var obj = $firebaseObject(ref);
obj.foo = "bar";
obj.$save().then(function(ref) {
  ref.key() === obj.$id; // true
}, function(error) {
  console.log("Error:", error);
});
}

edit

To desync a firebase object/array you can follow this answer: How to disassociate angularFire bound object?

Community
  • 1
  • 1
Michelangelo
  • 5,888
  • 5
  • 31
  • 50
  • I think the $save() function will automatically update all the connected clients. – Ant May 15 '15 at 21:54
  • What do you mean? You are overcomplicating things...https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-save – Michelangelo May 16 '15 at 10:02
  • When the $save function is triggered, firebase propagate these changes to all connected browsers, behavior I would like to control like this : propagate only to FirebaseArray loaded by an admin user, which does not seem to be implemented at the moment. So I guess the only way to achieve this logic would be on the client side to ignore propagated data, which I am not sure how to achieve. – Ant May 16 '15 at 16:36
  • 1
    @Cooluhuru What is the point of having a realtime backend if you don't want this behaviour? But I am not sure if you ever read through the docs but it is right there: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-destroy and here an older answer but kind of relevant for your problem http://stackoverflow.com/questions/18144812/how-to-disassociate-angularfire-bound-object – Michelangelo May 17 '15 at 21:06
  • Hi @Mikey, thank you for the SO link about the disassociate() function. Please copy paste the answer in the SO link, and I'll mark it as resolved. I have read the firebase documentation several time before asking this question, and I didn't realized that the ref() function passed in the promise did dis-associate the real-time synchronization. I have explained why in some cases I want to use the real time firebase storage, but want to disable the real time updates for some users. – Ant May 18 '15 at 02:12
  • @Cooluhuru There you go. Fixed my answer. Ok, yes sometimes functionallity is burried in the docs. I agree with you that it is hard to find sometimes. – Michelangelo May 18 '15 at 08:52