Two way Data Binding vs One way Data Binding.

RanveerSequeira
4 min readDec 16, 2023

--

htmx is better.

To understand this first we’ll need to learn about Model-View-Controller pattern(MVC). Let’s learn it while making pizza from ChatGPT.

The pizza-making process can be split into three parts:

  1. The Chef (Model): This is the person who decides what ingredients to use and how to put them together. They handle the dough, the sauce, the cheese, and all the toppings.
  2. The Table (View): Think of this as the plate where the pizza ends up. It’s what you see and use to enjoy your pizza. The table doesn’t make the pizza; it just presents it to you in a nice way.
  3. The Server (Controller): This person takes your order, tells the chef what you want, and brings the pizza to your table when it’s ready. They communicate between you (the person who wants the pizza) and the chef (who makes the pizza).

So, in computer talk:

  • The Chef is like the part of the computer program that works with data and does all the “cooking.” It’s where things happen and get prepared (like how the model manages data and business logic in the program).
  • The Table is what you see and interact with, just like how a web page or an app shows you information. It’s the part you can touch and use (like the view, which displays the data to the user).
  • The Server is the one who takes your requests and makes sure things get done. It’s the middle person that connects what you see and what’s happening behind the scenes (like the controller, which manages user requests and interacts with both the model and the view).

Two-Way Data Binding:

Imagine you’re at a pizza place where the chef and the server are connected by a magic phone. You (the customer) can call the server and tell them what kind of pizza you want, and the server relays this directly to the chef. But here’s the special part: if the chef changes the toppings or something about the pizza, the server immediately tells you about it, almost like the pizza talks back to you! So, if you wanted pepperoni and the chef decided to add mushrooms instead, the server would tell you about this change right away.

In computer terms, this is like a system where if the data (pizza order) changes in one place (chef), it automatically updates in another place (you, the customer), and vice versa. It’s a two-way communication where changes in the model (chef) immediately reflect in the view (you) and changes in the view also update the model.

One-Way Data Binding:

Now, let’s say you order a pizza through a waiter, and the waiter notes down your order but doesn’t tell you anything else until your pizza arrives. You’ve given your order, and you have to wait until it’s ready. Even if the chef changes something, you won’t know until the waiter brings your pizza to the table. There’s only one direction of communication — from you to the waiter and then from the waiter to the chef. You’re not constantly updated on the pizza’s progress.

In computing terms, this is like a system where data flows in one direction: from the model (chef) to the view (you) or from the view to the model, but not both simultaneously. Changes in the model update the view, but changes in the view don’t directly update the model in real-time; they usually go through a process or an action to make changes in the model.

So, in short:

  • Two-Way Binding is like a live conversation where changes in one place immediately affect the other.
  • One-Way Binding is like giving an order and waiting for it to be fulfilled without constant updates on its progress.

Lets get into boring stuff.( i don’t know angular)

Angular (Two-Way Data Binding):

import { Component } from '@angular/core';

@Component({
selector: 'pizza-form',
template: `
<input [(ngModel)]="pizzaTopping" placeholder="Enter Topping">
<p>You ordered: {{ pizzaTopping }}</p>
`
})
export class PizzaFormComponent {
pizzaTopping: string = '';
}

In this Angular example, [(ngModel)] is used for two-way binding. When you type in the input field, the value automatically updates in the pizzaTopping variable. Similarly, if pizzaTopping changes elsewhere in the code, the input field is updated too. It's like a live connection where changes in the input field immediately update the variable and vice versa.

React (One-Way Data Binding):

import React, { useState } from 'react';

function PizzaForm() {
const [pizzaTopping, setPizzaTopping] = useState('');

const handleInputChange = (e) => {
setPizzaTopping(e.target.value);
};

return (
<div>
<input
value={pizzaTopping}
onChange={handleInputChange}
placeholder="Enter Topping"
/>
<p>You ordered: {pizzaTopping}</p>
</div>
);
}

In React, the input field value is controlled by value={pizzaTopping} and updates are managed by the onChange event. Changes in the input field trigger the handleInputChange function, updating the pizzaTopping state. However, this change doesn't directly reflect back to the input field. Instead, it follows a one-way flow: from the input to the state and then to the UI.

Angular’s two-way binding allows direct synchronization between the model (data) and the view (UI), while React opts for a more controlled one-way data flow, where data changes trigger updates but don’t automatically propagate back to the source.

--

--