Forums

Posting in these forums is disabled. These forums will be available for archive purposes. Please join the new forums at the links below:

  • yui-support - replaces the `YUI 3.x` and `YUI 3 Gallery` forums.
    We have created the following discussion categories within this group to aid discoverability for these most-used topics:
    • Charts for YUI Charts support.
    • DataTable for YUI DataTable support.
    • Gallery for YUI Gallery support, including support for published Gallery components as well as the Gallery process in general.
    • Tools for support of YUI’s suite of developer tools such as selleck, shifter, grover, yogi, etc.
    • Everything Else for questions that don’t fit one of the categories above, we’ve got you covered here.
  • yui-deprecated - replaces the `YUI 2.x` forum and the forums of other deprecated products (`YUI Doc`, `Builder`, `YUI PHP Loader`, etc.).
  [ 4 posts ]
New Topic | Post Reply | Print view
Previous topic | Next topic

Raphaël Bauduin

  • Username: raphinou
  • Joined: Thu Nov 26, 2009 7:03 am
  • Posts: 39
  • GitHub: raphinou
  • Gists: raphinou
  • IRC: raphinou
  • Offline
  • Profile
Tags:
  • io
  • module

Is a module the solution for a function to be re-used ?

Post Posted: Fri Jan 22, 2010 1:53 am
Hi,

I have a javascript function called get_detail_validator that I'm using in multiple location in my app, each time in a YUI.use block.
This method uses Y.io, which has as consequence that I cannot define it as a plain function in my js file.

The solution I've used is to create a module for my app modeled after the yql module (http://github.com/davglass/yui-yql/blob/master/yql.js )

Code:
YUI.add('madb', function(Y) {
 if (!YUI.madb_forms) {
   YUI.madb = {};
 }

var get_detail_validator= function (detail_id) {
    // blabla
    Y.io(check_url, cfg);
    //blabla
}

  YUI.madb = { get_detail_validator: get_detail_validator }
}, '1.0', { requires: ['io-base'], skinnable: false} );


With this in place I can call the method

Code:
YUI.madb.get_detail_validator(345);


Can someone confirm this is the right way to go?

Thanks in advance

Raph

Daniel Cook

  • Offline
  • Profile

Re: Is a module the solution for a function to be re-used ?

Post Posted: Fri Jan 22, 2010 8:13 am
I would not add it to the global YUI but rather to the local Y instance (Y.madb). That way it will only be used within the 'use' method.

Minor question, why do you check for 'madb_forms'?

Satake

  • Joined: Tue Jul 07, 2009 9:15 am
  • Posts: 51
  • Offline
  • Profile
Tags:
  • examples
  • io
  • substitute
  • yui3

Re: Is a module the solution for a function to be re-used ?

Post Posted: Fri Jan 22, 2010 10:12 am
in the first part of the YQL stuff dav is defining a global object for callbacks so I don't think you need this part

if (!YUI.madb_forms) {
YUI.madb = {};
}

and daniel is correct you would want to add your function to the Y instance not directly to the YUI global variable so the module stuff can manage it.

You can use the namespace function to make a hole for yourself that won't overwrite stuff, in case you want to incrementally add more functions to that madb namespace

var get_detail_validator = function(...) { ... };

Y.namespace('madb'); // makes Y.madb object if it doesn't exist

Y.madb.get_detail_validator = get_detail_validator;

then in the use statement for an instance where you want to use it:
Code:
YUI({
   'madb':{
      fullpath:'/path to the stuff',
      requires: ['io-base']
   }
}).use ('madb', function(Y) {
   // io-base and madb available in Y via hot combo loader action
   Y.madb.get_detail_validator(...) { };
   
});


substitute does a similar deal as well.

FYI there are some form modules in the Gallery under F maybe someone has already done what you need.

Raphaël Bauduin

  • Username: raphinou
  • Joined: Thu Nov 26, 2009 7:03 am
  • Posts: 39
  • GitHub: raphinou
  • Gists: raphinou
  • IRC: raphinou
  • Offline
  • Profile
Tags:
  • examples
  • io
  • substitute
  • yui3

Re: Is a module the solution for a function to be re-used ?

Post Posted: Sat Jan 23, 2010 2:51 am
Satake wrote:
in the first part of the YQL stuff dav is defining a global object for callbacks so I don't think you need this part

if (!YUI.madb_forms) {
YUI.madb = {};
}


Thanks for the clarification.

Satake wrote:
FYI there are some form modules in the Gallery under F maybe someone has already done what you need.


Yes, this code is using gallery-form by Greg Hinch. It's working great indeed.

Raph
  [ 4 posts ]
New Topic | Post Reply | Print view
Previous topic | Next topic
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