Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PubSub/Loosely Coupled & MVC patterns in Javascript

Ok, i'm a newbie in this, i've been trying to study MVC patterns and Publish/Subscription patterns in Javascript/jQuery, however i believe that i haven't grasped the concept entirely.

Since i've been doing it alone, i humbly come here to ask for opinions about a small educational exercise that i tried to make following these patterns

http://jsfiddle.net/Couto/R62V8/

Ok, the exercise is mainly a login form, where the values are saved in localStorage, again it's purely educational, i now it's not safe in anyway and it should not be used in production.

Would you please tell me your opinion about the patterns used, did i fail at achieving the right use of patterns? Please hurt me if needed, i just want to learn, but i'm not sure if i'm doing it right.

like image 685
Couto Avatar asked Jan 19 '11 03:01

Couto


1 Answers

jQuery's event delegation system is unto itself a form of pub/sub. In fact check this out, http://bugs.jquery.com/ticket/7547. You'll see under the hood it uses the event system and really only changes the naming scheme and works on a "global" context.

I have nothing against pub/sub, but feel you're adding a layer that doesn't need to exist in certain cases. Would triggering the correct function immediately after the event delegation fires rather than triggering a pub really be worse? If you remove the subscribe and the publish you'd wind up with nearly the same code without the added "complexity".

In other cases the publish/subscribe make a lot of sense. Your display/hash and set/login make a lot of sense as they aren't part of a traditional event that other code could subscribe/bind to.

One could make the argument that by using pub/sub everywhere, including in event delegation, you're abstracting away your reliance on outside code from having to write event delegations of their own, which removes their worry about what element to bind the delegate to. If you're writing big complex applications, go ahead and abstract it out to that level. If you're not planning on writing big applications weigh the benefits cause YAGNI might apply here.

like image 200
Akkuma Avatar answered Oct 03 '22 08:10

Akkuma