| Page 1 of 1 | [ 2 posts ] |
|
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); }); |
|
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. |
| Page 1 of 1 | [ 2 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group