site-wide/"globally used" behaviors (best practice)

21 views
Skip to first unread message

Rolf-nl

unread,
Feb 27, 2012, 5:36:53 PM2/27/12
to Clientcide
If you have some site-wide code/classes you want to use "globally",
(eg. LogSystem, CookieMonster, Storage, SuperFancyFX) are you adding
these to the dom in the first element after <body> for example, or
how?

That is what I have now, like using a wrapper div that's already
present for this behavior container <body><div id="wrapper" data-
behavior="Prefs Storage">...</div></body>

Then in other behaviors I use api.fireEvent('ThisBehaviorUpdated',
[foo, bar]) to broadcast and some behavior declared in this wrapper-
div can pick stuff up whenever it's needed.

Makes sense? Or wrooooong implementation?

Rolf

Aaron Newton

unread,
Feb 27, 2012, 5:57:16 PM2/27/12
to clien...@googlegroups.com
Behaviors generally speaking are used only to decorate elements on the page. If you have a behavior that needs to refer to some global property (user data for example) I'd use the passMethod/passMethods argument to pass in getters for them to your filters. So your filter can do api.get('userdata') or whatever. Now you can easily search for and support that API.

But putting behaviors on the document body doesn't really get you anything (unless you are really decorating the body). Instead, I'd instantiate these things and then pass them to behavior. Like so:

var user = new User(...);
var storage = new Storage(...);

var b = new Behavior();
b.passMethods({
  getUser: Function.from(user),
  getStorage: Function.from(storage)
});
b.apply(document.body);


Rolf

--
You received this message because you are subscribed to the Google Groups "Clientcide" group.
To post to this group, send email to clien...@googlegroups.com.
To unsubscribe from this group, send email to clientside+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/clientside?hl=en.


Rolf-nl

unread,
Feb 28, 2012, 3:41:07 PM2/28/12
to Clientcide
before closing textmate yesterday evening I added "//check Behavior
Method: passMethod {#Behavior:passMethod}" to some of my testing
crapness so I was sort of on a right track but not there yet. Your
last example seems much more usable indeed; I was using an element to
instantiate something for the sake of it being the "parent" element..
wrooong!

More later
Reply all
Reply to author
Forward
0 new messages