Backbone - NestedModels - Issues with nested sets not triggering 'change' event

I have incorporated the Backbone nested plugin into my project. The way I set up my binding is as follows :

var view = Backbone.View.extend({
    initialize: function(params) {
        this.model.bind('change', _.bind(this.rerender, this));
    }

The model I assigned to the view is defined in global scope outside the view like this :

newModel = new Backbone.NestedModel(jsonData);

When I use newModel.set("prop", "value"), it triggers a change event and prompts a rerender.

However, if I use newModel.set("prop.prop", "value") and the inner property already exists, it does not trigger the "change" event. This means that a value change is not detected.

Interestingly, a new value is detected: newModel.set("newProp.newProp", "value") triggers a change.

The nested gets DO WORK.

When I retrieve a value using newModel.get("prop.prop"), it returns the desired value.

Furthermore, if I specifically listen to a certain property, it works as expected :

this.model.bind('change:prop.prop', _.bind(this.rerender, this));
} //fires rerender

According to the documentation of the plugin, listening to "change" should trigger a change in any scenario :

// this will fire when 'name.middle.initial' is set or changed

user.bind('change', function(model, newVal){ ... });

However, in my case, it does not work as expected. What could I have possibly done wrong?

Answer №1

Have you considered delegating the event to the view using the listenTo method?

Here's a suggestion:
- When using the listenTo method, you can pass the context as the fourth parameter. This eliminates the need to bind the callback function, which can improve performance by avoiding unnecessary function calls.

For example:

var view = Backbone.View.extend({
    initialize: function(params) {
        this.listenTo(this.model, "change", this.rerender, this);
     }

I hope this solution proves helpful to you.

Cheers!

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

The feature to select a cell on a Bootstrap table is not functioning as expected

Is there a way to retrieve the Cell value from a Bootstrap table when clicked? I have a method set up, but for some reason, after clicking on the table heading, the tdOnClick() function stops working. It seems to only be functional before clicking on the t ...

Struggling to make JavaScript read JSON data from an HTML file

I am currently working on developing a word search game using django. One of the tasks I need to accomplish is checking whether the entered word exists in a dictionary. To achieve this, I have been converting a python dictionary into JSON format with json. ...

No response observed upon clicking on the li element within the personalized context menu

I created a custom context menu that pops up when you click on an li element within an unordered list. I'm trying to trigger an alert when clicking on an li item inside the context menu, but it's not working as expected. To handle this dynamic c ...

Counter is effective for the initial post, yet it does not function properly for the subsequent post

Can anyone help with an issue I'm having with my JavaScript counter? It works for the first comment, but not the second one. This problem persists whether I use PHP or JavaScript. Below is the JavaScript code for the counter: var count = (function() ...

Using the useRef hook to target a particular input element for focus among a group of multiple inputs

I'm currently working with React and facing an issue where the child components lose focus on input fields every time they are re-rendered within the parent component. I update some state when the input is changed, but the focus is lost in the process ...

Load JSON file without caching in either next.js or node.js

I recently developed an API using next.js. To supply data, I chose to use a JSON file which I imported as a module. However, I noticed that even if the JSON file content changes, the server still displays the original content from when it was first started ...

Are these objects enclosed within a JavaScript array?

Are square brackets used to define arrays and curly brackets used for objects? Can you explain the following data structure: Some.thing = [ { "swatch_src" : "/images/91388044000.jpg", "color" : "black multi", "inventory" : { "F" : [ 797113, 797 ...

Exploring the ancestry of Mongo

I am currently working on constructing a family tree that can have an infinite number of levels for parents and children. I am also interested in finding relationships like brothers, sisters, cousins, and so on. However, I'm facing some confusion when ...

Switching between a secondary menu using ClassieJS or jQuery?

Currently, the code is configured to toggle the same menu for every icon. To see my current progress, you can check out this fiddle: http://jsfiddle.net/2Lyttauv/ My goal is to have a unique menu for each individual icon. I began by creating the followi ...

Using Node.js and Hapi.js alongside Angular.js for web development

Could someone please help me understand how to integrate nodejs (hapi server) with AngularJs? I initially thought that I could intercept all requests made to my Hapi server and handle them using angularjs routes / REST, but it seems like I'm encounter ...

When moving the cursor quickly, a vertical line does not appear upon hover

I am facing an issue with the vue-chartJs library. When I move the cursor fast, the vertical line on hover does not show up. However, when I move the cursor slowly, it works perfectly. Can anyone offer assistance in solving this problem? onHover: functi ...

Selecting the quartile value for every individual data point

I am currently graphing the sentiment values of tweets over a span of 10 years. The CSV file contains three columns as outlined below. Successfully, I managed to plot each value by date. However, upon attempting to create an area graph, I encountered an i ...

JSON module in Python throws an error despite the input being accurate

Working on a group project, I'm faced with the task of importing a JSON object using python. The issue comes up when Python consistently throws an error message at me, despite the fact that the JSON string is accurate (validated it using JSONLint). Be ...

Optimal method for retrieving data from a JSON object using the object's ID with a map

Can you teach me how to locate a json object in JavaScript? Here is a sample Json: { "Employees" : [ { "userId":"rirani", "jobTitleName":"Developer", "preferredFullName":"Romin Irani", "employeeCode":"E1", "region":"CA", "phoneNumber":"408-1234567", " ...

Searching for a specific data point within the latest entry of a JSON file using Javascript

I am currently in the process of utilizing a sensor to measure temperature, which is then stored in a mongo database. This data can be accessed as a JSON file by visiting ('/data'). My goal is to determine the latest entry and extract the temper ...

Snowflake's innovative solution for converting semi-structured data into a flat format without the need for key-value

I currently have JSON data stored in an array format within Snowflake, structured as: [[col1, col2, col3, col4], [a1, a2, a3, a4], [b1, b2, b3, b4], [c1, c2, c3, c4]... etc] This array is loaded into Snowflake as a table named data_table, with 1 row (rep ...

What is the best way to eliminate concealed divs from the view source of a webpage?

On my HTML page, I have some hidden DIVs that can still be viewed in the page source. I want to ensure that these DIVs are not visible to users when they inspect the page source. Is there a way to achieve this using Javascript or another solution? ...

Tips for accessing the current state/value in a third-party event handler?

Consider the following scenario: function MapControl() { const [countries, setCountries] = useContext(CountriesContext) useEffect( () => { ThirdPartyApi.OnSelectCountry((country) => { setCountries([...countries, country]) }) }) ...

Custom-designed background featuring unique styles

I have implemented the following code to create a continuous running banner: <style> #myimage { position: fixed; left: 0%; width: 100%; bottom: 0%; background:url("http://static.giga.de/wp-content/uploads/2014/08/tastatur-bild ...

The `innerHTML` function is responsible for closing HTML

My switch structure is set up as follows: switch (ratio) { case "16:9": imgContainer.innerHTML = '<img src="images/test-rata-de-aspect.jpg">' break case "4:3": imgContainer.innerHTML = '<img src="image ...