[ 2 posts ]

Nizzy

  • Username: nizzy
  • Joined: Mon Dec 14, 2009 4:32 pm
  • Posts: 17
  • Offline
  • Profile

Inheritance Pattern, pls help

Post Posted: Fri Jun 25, 2010 2:22 pm
+0-
I am trying to build custom module which will be extended by other objects. I am not sure I am in the right direction in terms of inheritance pattern. Please help me.

Here is what I am trying to do:
Scenario 1: User (visitor) visits a page where I load shared.js and user.js. User extends onto Shared.
Scenario 2: Admin visits the administrator page where I load shared.js, user.js and admin.js. Admin extends onto User which extends onto Shared accordingly.
* I also need to use some of the YUI2 libs.

Code:
// shared.js
YUI.add('shared-lib', function(Y) {
   var Shared = function() {
      // some code
   };

   Y.extend(Shared, Y.Base);
}, '0.0.1', { requires: ['base', 'some-other-libs'] });

// user.js
YUI().use('shared-lib', function(Y) {
   var User = function(cfg) {
      User.superclass.constructor.apply(this, arguments);

      // some code
   };

   Y.extend(User, Shared);

   var user = new User(cfg);
});

// admin.js
YUI().use('shared-lib', 'yui2-some-old-libs', function(Y) {
   var Admin = function(cfg) {
      Admin.superclass.constructor.apply(this, arguments);

      // some code
   };

   Y.extend(Admin, User);

   var admin = new Admin(cfg);
});

Nick Husher

YUI Contributor

  • Offline
  • Profile

Re: Inheritance Pattern, pls help

Post Posted: Mon Jun 28, 2010 12:16 pm
+0-
You are indeed extending in the right direction. Y.extend(ObjectYouWantToExtend, ObjectToExtendFrom). There's an optional third argument if you want to do some fancy prototype merging stuff.

A note on the sample code you've provided: admin.js doesn't know about the User object: you'll need to define User as a module.
  [ 2 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