[ 3 posts ]

Prashant Mothukuri

  • Username: prashantm
  • Joined: Wed Jun 27, 2012 3:03 am
  • Posts: 5
  • Offline
  • Profile
Tags:

Using YUI in global functions.

Post Posted: Thu Jun 28, 2012 5:55 am
+0-
I am using global functions in my js file and inside those functions I am using YUI by adding YUI.use(module, function(){}). This I have to add in all the functions. I want to know if this is the right design. Every time my function is called, it will create a new instance of YUI is what I believe. And that will be in memory till it is garbage collected.

Also by doing this my function will take extra processing time for creating YUI instance. Is it right to create a global Y object and use it in the functions. I tried that but I wouldn't know if all the modules specified in YUI.use are loaded or not when my function is called as they happen asynchronously. This will happen when I am calling the function on page load.

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 617
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile
Tags:

Re: Using YUI in global functions.

Post Posted: Thu Jun 28, 2012 10:29 am
+0-
Your question is hard to answer because it's based on something most of us perceive as a bad practice (the use of global functions). In fact, YUI 3 was designed to avoid doing anything in the global scope.
prashantm wrote:
I want to know if this is the right design

So an honest answer to your question would be that it's not the right design to use YUI in global functions. In order to avoid (temporarily) all the problems you correctly figured out, you can use the Configurator to get a copy of YUI and synchronously create a global Y with all your required modules.

Nate Cavanaugh

YUI Contributor

  • Offline
  • Profile
Tags:

Re: Using YUI in global functions.

Post Posted: Fri Jul 06, 2012 11:24 am
+0-
In general, globals are bad, but there are times when it's unavoidable (for whatever reason), and if you can guarantee that the function name is unique, there's rarely any issue.

One thing I created (because we had a lot of these issues with legacy code) was to create YUI.provide: https://github.com/natecavanaugh/yui-provide

Basically, what id does is allows you to create functions that will load their dependencies, but will only go through the YUI use once, and it will do it only when the function is called.

So if you define a function, but never call it on that page, it won't download any dependencies.

The first argument to the provide function is an object, so you can use this for any method, even ones that are namespaced to a specific object.

HTH,
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum