Mohit Jain
Mohit Jain

Reputation: 43929

What's the big picture difference between observers and callbacks?

What's the big picture difference between observers and callbacks? When and where to use which one?

Upvotes: 7

Views: 1370

Answers (3)

Jakub Hampl
Jakub Hampl

Reputation: 40533

I thing it's a lot about keeping your models reasonably lean. So when you find your model getting ridiculously long it might be time to refactor it to use an observer.

From the strict architectural POW (as noted in John's answer) it is about separation of responsibilities but if it's a simple thing I prefer to stick it in the model anyway.

Technically there is practically no difference.

Upvotes: 0

John Topley
John Topley

Reputation: 115322

It's about separation of concerns.

Observers allow you to factor out code that doesn't really belong in models. For example, a User model might have a callback that sends a registration confirmation email after the user record is saved, but you don't really want this code in the model because it's not directly related to the model's purpose.

Observers allow you to have that clean separation because you don't have all that callback code in your models. Observers depend a model (or models), not the other way around.

Upvotes: 11

shingara
shingara

Reputation: 46914

For me it's the same thing. But I prefer callback instead of Observer.

Observer are callback but in separate model.

Callback are inside the model. So you see there are callback and you can hack it.

Upvotes: 0

Related Questions