Is it possible to integrate a backbone model with Angular?

Below is an example of a Backbone post model:

var Post = Backbone.AssociatedModel.extend({
    urlRoot: ApiService.resolveRESTUrl('posts'),
    defaults: {
        age : 0,
        fname : "",
        lname : "",
        manager : null
    },
    relations:[
        {
            type:Backbone.One,
            key:'User',
            relatedModel: function(){ return $injector.get('UserModel') },
            collectionType: function(){return $injector.get('UserModel').Collection; }
        },
        {
            type:Backbone.Many,
            key:'Last3Comments',
            relatedModel:function(){ return $injector.get('CommentModel')  },
            collectionType:function(){ return $injector.get('CommentModel').Collection },
        }
    ],
    getTimeAgo:function() {},
    getPicture:function(size){
        return this.get('picture_url') ?
            ApiService.getImageResizeUrl(this.get('picture_url'),'w'+size+'xh'+size) :
            null;
    },
});

While loading posts, if any library using Angular watcher in the post array (binded with "="), I encounter the following error:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []

Additionally,

RangeError: Maximum call stack size exceeded at Array.toString (native)

I am monitoring posts in this manner:

$scope.posts=[];
$scope.$watch('posts', function(model) {
    console.log($scope.posts, model);
},true);
$scope.loadPosts=function(){
    if($scope.loading || $scope.disabled) return;

    $scope.loading=true;

    ApiService.request("posts/home",{page:$scope.page,limit:10}).success(function(data){
        $scope.loading=false;
        $scope.page++;
        if(data.error){
            alert('Error');
        } else {
            if(data.data.posts.length==0)
                $scope.disabled=true;
            $.each(data.data.posts,function(i,e){
               var post=new PostModel(e);

                $scope.posts.push(post);
            });
        }
    }).error(function(){
        alert('Error');
        $scope.loading=false;
    });
};

Any thoughts on how to resolve these errors?

Answer №1

After hours of troubleshooting, I finally found the solution by leveraging angular's watchCollection instead of using just watch. WatchCollection is specifically designed to monitor changes in arrays.

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

Tips for incorporating property into an object using ng-model in an HTML page with AngularJS

During my ng-repeat iteration, I am trying to add each field to another object using ng-model. //html <div ng-repeat="field in forms.fields track by $index"> <span ng-if="field.type=='textbox'"> <span style="width:115px; ...

Using Jquery to create an array containing all the items in the pager

192.168.1.1/home?page=1, 192.168.1.1/home?page=2, 192.168.1.1/home?page=3. Is there a way to create an array of only the new items on the pager? I am interested in storing only the elements with the class item-new into the array. To clarify further: I n ...

The repairDatabase function cannot be found in the Collection.rawDatabase() method

Seeking guidance on repairing a database within Meteor. Currently executing the following code: Meteor.methods({ 'repairDB'(){ Users.rawDatabase().repairDatabase(); return true; } }); Encountering the following error: I20170630-18: ...

The counterpart to Ruby's `.select{ |x| condition }` in Javascript/ React.js would be to

This javascript function in React.js utilizes a for loop to determine the opponent team: getOpponentTeam: function(playerTeamId){ var matches = this.state.matches; var player_team = this.state.player.team.name for (i in matches){ if (matches[i]. ...

Issue with popup display in React Big Calendar

I'm currently working on a calendar project using React-Big-Calendar, but I've run into an issue with the popup feature not functioning properly. <div className={styles.calendarContainer} style={{ height: "700px" }}> <C ...

Create a dynamic animation page using Node.js, then seamlessly redirect users to the homepage for a smooth user

Good day everyone! I've decided to change things up with my latest query. I'm currently working on adding a loading page animation that will show for 3 seconds when visiting the '/' route, and then automatically redirect to the home pag ...

What is the proper way to reference a property's value within another property of the same JavaScript Object?

Within a Gulp.js file (or any Javascript file), I have defined paths in a Javascript object: var paths = { devDir : 'builds/development/', devDirGlobs : this.devDir+'*.html' } I am attempting to reference the pro ...

Utilizing ReactJS useState to eliminate a className

Basically, I'm trying to remove a className from an element when a button is clicked. I've attempted to use useState hooks and a regular function, with the onClick event on the button triggering this function/setUseState. However, the className l ...

Troubleshooting a Node.js problem with variable scope

I'm working on a nodejs route that downloads a URL as an mp3 using npm-youtube-dl. I have a download directory being monitored with chokidar for new files, and once a file is added, I save the file link. After the download completes, a function is cal ...

Writing the success function for a jQuery ajax call involves defining the actions to be taken once

Embarking on my journey to learn jQuery and web development, I am faced with the task of sending user input (username and password through a submit button) to a PHP page using .ajax and success function. Below is the HTML form code: <form id="form1"&g ...

running a prompt command from my PHP/HTML script

I currently run a puppeteer program by typing c:\myProgram>node index.js in the command prompt. However, I would like to automate this process through my PHP program instead of manually entering it each time. Something similar to this pseudo-code ...

Combining query results/objects by id in an array within a React application with Firebase Firestore

After retrieving chat messages from a Firestore snapshot, I have the following query result involving conversations among three individuals: {timestamp: "October 25th 2020, 11:13:59 am", name: "John Doe", email: "<a href="/cdn ...

The nested directive link function failed to execute and the controller was not recognized

Apologies in advance for adding to the sea of 'mah directive link function isn't called!' posts on Stack Overflow, but none of the solutions seem to work for me. I have a directive named sgMapHeader nested inside another directive called sg ...

Designing an interactive HTML table that adapts to various screen

Currently utilizing Bootstrap to create a responsive HTML table on smaller devices like the iPad, but seeking a more polished and professional alternative. Searching for a JQuery/JavaScript or CSS solution without relying on plugins. Would appreciate any ...

Localization of text in jQuery timeago.js

I have implemented J Query time ago to display date and time on my website. I am currently working on a multilanguage website where I want the time ago message to show as "1 min ago" for English users and "1 دقیقه قبل" for Farsi users. Can I achi ...

What is the process for reverting variables back to their original values using pure Javascript?

I am working on developing a hangman game using vanilla javascript and I need some assistance with resetting the game after a player loses. Specifically, I would like to: 1. Reset the "guessRemain" variable. 2. Clear out the "guess" id so that none of the ...

Looking to incorporate AAD calling functionality in a React and Node application by utilizing the Graph API for communication/calls

As a newcomer to Microsoft Azure and its services, I recently registered an application with Azure. However, when attempting to integrate a call feature in my Web App using the graph API '/communication/calls', I encountered the following error m ...

Does the AngularJS Controller disappear when the DOM element is "deleted"?

One challenge I encountered is regarding a directive that is connected to an angularjs controller, as shown below: <my-directive id="my-unique-directive" ng-controller="MyController"></my-directive> In the controller, at a certain point, I ne ...

The express response fails to include the HTML attribute value when adding to the href attribute of an

When using my Nodejs script to send an express response, I encounter a problem. Even though I set the href values of anchor tags in the HTML response, they are not visible on the client side. However, I can see them in the innerHTML of the tag. The issue ...

What is the best way to obtain a reference to the parent form element within an AngularJS directive?

Here is the HTML code I am working with: <form name="my-form"> <div class="col-sm-4"> <input type="phone" name="mobileNumber" ng-model="form.mobileNumber" value="0439888999" required> </div> </form> In addition, I ha ...