开发者

Javascript Class Model

开发者 https://www.devze.com 2022-12-16 02:52 出处:网络
I\'m working on creating a Javascript Tab library. Actually, it\'s already been developed and is working great. But it\'s due for a rewrite to fix some underlying annoyances and quirks we\'ve found wh

I'm working on creating a Javascript Tab library. Actually, it's already been developed and is working great. But it's due for a rewrite to fix some underlying annoyances and quirks we've found while using it. Anyway, here's the current model.

The current model has a TabSet object, which houses the main functions for the tab library: addTab, removeTab, showTab, hideTab, and related history functions. Then there is a Tab object that contains the data/methods related to the tab: showThrobber, hideThrobber, reload, and creating the actual DOM elements for the tab. Now, you can see this is a bit disjointed. TabSet handles showing a tab and hiding a tab and Tab handles reloading the t开发者_运维知识库ab.

Here's the question: What is the best way to organize the methods for this tab library. The problem we are running in to is that the current model, while disjointed, makes sense. TabSet is indeed showing a tab, hiding a tab and removing a tab. But the Tab itself is indeed being shown, hidden and removed. Really, all the function make sense to be in either class: TabSet or Tab.

Let's use an analogy. When someone needs to talk, everyone needs to stop talking. There are two ways for this to happen. 1) The leader (TabSet) tells everyone to stop talking and then tells the speaker to start talking. 2) The speaker tells everyone to shutup and starts talking. It makes sense to have the controller tell other Tabs to hide and tell the new Tab to show. But it also makes sense to have the Tab tell all the other Tabs to hide and then show itself.

What are your thoughts?


Personally, I'd design it so that a Tab only knows about itself and the TabSet that is managing it. It wouldn't carry a reference to the other tabs.

I'd put show() method on Tab; internally it would ask the TabSet to hide all other tabs and show itself.

I'm assuming exactly one tab must be shown at all times, so there's no point in having a public method to hide an individual tab.

The idea behind that design is that you have less error checking to worry about. If your TabSet object had a showTab(tab) method, you'd have to check to make sure that the tab being passed in was actually one of the tabs in the set.

Usually when I design an API my first priority is to make it hard or impossible to pass in invalid inputs.

0

精彩评论

暂无评论...
验证码 换一张
取 消