When new references are loaded in AngularFire 0.5.0, the $on loaded function is triggered just

Imagine we are utilizing the pushState to navigate through routes:

$locationProvider.html5Mode(true);

We have two routes with different controllers:

$stateProvider.state('one', {
  url:'/one',
  templateUrl:'one.html',
  controller: 'oneCtrl'
});

$stateProvider.state('two', {
  url:'/two',
  templateUrl:'two.html',
  controller: 'twoCtrl'
});

The oneCtrl creates a new instance of the same resource as twoCtrl:

.controller('oneCtrl', ["$scope", "$firebase", function($scope, $firebase) {

  var oneRef = new Firebase("https://example.firebaseio.com/stuff");
  $scope.stuff = $firebase(oneRef);
  $scope.stuff.$on("loaded", function(value) {
    console.log(value);
  });
}])

.controller('twoCtrl', ["$scope", "$firebase", function($scope, $firebase) {

  var twoRef = new Firebase("https://example.firebaseio.com/stuff");
  $scope.stuff = $firebase(twoRef);
  $scope.stuff.$on("loaded", function(value) {
    console.log(value);
  });
}])
  • When navigating to /one, the $on("loaded") event triggers as expected for the oneRef.
  • If we then proceed from /one to /two, the $on("loaded") event does not trigger.

If a new reference was created - twoRef - why is the callback not firing a second time?

Answer №1

The reason for this behavior is that locally cached data triggers value events synchronously in Firebase.

Upon the initial load, angularFire's _getInitialValue method contacts the server asynchronously. However, on subsequent loads, since the value is already stored locally, the broadcast event occurs synchronously, potentially before your $on('loaded'...) handler is attached.

It might be advisable for controllers using the same $firebase data to be consolidated into one, likely as a shared service.

An alternative solution could involve maintaining the loaded state within a service or the $rootScope.

In the long run, it should ideally be addressed in angularFire to ensure that the loaded event fires consistently even when the 'value' is already loaded synchronously. Check out the discussion at this GitHub thread for more details.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transferring information between a service and a controller

I'm struggling to pass data between a service and a controller in my AngularJS project. Even though I've followed the recommended steps, the code is not functioning as expected. Below is the controller snippet: function udpController($scope ...

The JQUERY Click event fails to trigger only on the initial click

While attempting to display the name stored as a data value for each button in the code, I encountered an issue where it wouldn't work on the first click event call. However, after the initial click, it operated normally. It is important to note that ...

A guide on updating a JSON object based on specific criteria

I'm dealing with two JSON files here - the first one is named origin.json, and the second one is called newData.json. My goal is to update the child value of Origin based on the data in NewData. However, I only want this update to impact items that a ...

React dynamically updating table with a fresh layout

With my React component, I've innovatively designed a block format for presenting To-Dos at a Pet Store. This allows users to categorize and organize their tasks in blocks rather than a simple list of unorganized To-Dos. The functionality of adding a ...

What is the best way to create a clickable color feature that triggers a function to change the fill of an SVG object when clicked by the user?

I am attempting to create a user-friendly color selection system where users can click on colors that will then be applied to fill an svg object, similar to a digital coloring book for children. I am struggling to figure out how to instruct JavaScript to s ...

Using jQuery each with an asynchronous ajax call may lead to the code executing before the ajax call is completed

In my jQuery script, I utilize an each loop to iterate through values entered in a repeated form field on a Symfony 3 CRM platform. The script includes a $.post function that sends the inputted value to a function responsible for checking database duplicat ...

At a certain point, the scrolling iframe within the mobile app suddenly jumps back to the top of the page

Looking for some assistance with my HTML code. Here it is: <header class="bar-title"> <a class="button-prev" onclick="history.back(-1)">back</a> <h1 class="title">Page</h1> </header> <div style="overflow:auto;-we ...

A guide on sending an array to an Express API

I have been working on creating APIs using Express.js and SQL Server. Posting an object was easy and simple, but now I have a new challenge: how do I post an array? Imagine I have a table that only stores two parameters: Table_A Id | CouponId In this ta ...

Detecting single letters in a sentence and changing their appearance using CSS

Looking to make a subtle change to text? I need to swap out single letters in a passage (I have a cat that ate a fish). Any ideas on how to do this? The goal is to input a block of text into a textbox, then display it in a div. I've had difficulty fi ...

Having trouble with Webpack loading angular-clipboard?

In my AngularJS project, I am utilizing Webpack. However, I encountered an error when attempting to load angular-clipboard. The error message displayed was: ERROR in ./assets/libs/angular-clipboard/angular-clipboard.js Module not found: Error: Cannot reso ...

Line numbers in Vue Codemirror

Currently, I'm working on integrating codemirror into my Vue.js application using a library found at https://github.com/surmon-china/vue-codemirror. The issue I'm facing is that even after importing and utilizing the codemirro component, everythi ...

What is the reason behind a Next.js Custom Server disabling Automatic Static Optimization?

I can't seem to grasp why the documentation mentions that having a custom server disables Automatic Static Optimization. Before opting for a custom server, it's important to note that it should only be used when Next.js integrated router doesn& ...

Issue with ThemeManager in Material UI & React: Constructor is not valid

Currently, I am integrating Material UI into a small React application, but I suspect that the tutorial I am following is outdated and relies on an older version of Material UI. The error _materialUi2.default.Styles.ThemeManager is not a constructor keeps ...

Managing jquery values with cookies for loading, saving, and resetting

I recently implemented screen adjustments on my website using scrollbars. I utilized this example as a reference. Below is the code snippet... HTML CODE: <h1>Image Editor with CSS Filters and jQuery</h1> <!--Form for collecting image URL ...

Combining two arrays without using concatenation, each from separate arrays but sharing one common variable

I'm struggling to combine two arrays in a specific manner and can't quite figure out the correct syntax to achieve this. primaryData = [1,2] secondaryData = [3,4] label = [label1, label2] Currently, I have this working data = $.map(la ...

Exploring Heroes in Angular 2: Retrieving Object Information by Clicking on <li> Items

Currently, I am delving into the documentation for an angular 4 project called "Tour of Heroes" which can be found at https://angular.io/docs/ts/latest/tutorial/toh-pt2.html. <li *ngFor="let hero of heroes" (click)="onSelect(hero)">{{hero.name}}< ...

Utilizing multiple local images effectively within a Next.js 13 component: Best practices for implementation

Currently working on a Next.js project and utilizing the Image component to showcase images. Familiar with importing a single local image like so: import Image from 'next/image'; import profilePic from '../public/me.png'; export defaul ...

Tips for implementing a slide up animation on a dynamic element

I have one div and a button. When the button is clicked, I am appending another div. After appending the second div, I want to add a slide-up animation to the first div and a slide-down animation to the second using jQuery. <div class="main"> & ...

What is the solution for changing image sources dynamically in React-Native?

How can I access image sources from a JSON file without encountering a module error when using JSON objects as the source? JSON FILE: { "Image": {"source": "./SourceFolder/ImageFile.png"} } JS FILE const data = require('./jason.json'); ...

What specific checks and alerts are triggered by React.StrictMode?

When utilizing React.StrictMode and React.Fragment, according to the React documentation: Both Fragment and StrictMode do not display any visible UI. Instead, they trigger additional checks and warnings for their child components. Question: What specif ...