Skip to content

Commit c7fac61

Browse files
gibson042dmethvin
authored andcommitted
Fix gh-51: New init method for subclasses, close gh-52
1 parent 74a500b commit c7fac61

File tree

2 files changed

+43
-55
lines changed

2 files changed

+43
-55
lines changed

src/core.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ jQuery.sub = function() {
9696
jQuerySub.fn.constructor = jQuerySub;
9797
jQuerySub.sub = this.sub;
9898
jQuerySub.fn.init = function init( selector, context ) {
99-
if ( context && context instanceof jQuery && !(context instanceof jQuerySub) ) {
100-
context = jQuerySub( context );
101-
}
102-
103-
return jQuery.fn.init.call( this, selector, context, rootjQuerySub );
99+
var instance = jQuery.fn.init.call( this, selector, context, rootjQuerySub );
100+
return instance instanceof jQuerySub ?
101+
instance :
102+
jQuerySub( instance );
104103
};
105104
jQuerySub.fn.init.prototype = jQuerySub.fn;
106105
var rootjQuerySub = jQuerySub(document);

test/core.js

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -193,64 +193,53 @@ test( "jQuery.sub() - .fn Methods", function(){
193193
expect( 378 );
194194

195195
var Subclass = jQuery.sub(),
196-
SubclassSubclass = Subclass.sub(),
197-
jQueryDocument = jQuery(document),
198-
selectors, contexts, method, arg, description;
199-
200-
jQueryDocument.toString = function(){ return "jQueryDocument"; };
201-
202-
Subclass.fn.subclassMethod = function(){};
203-
SubclassSubclass.fn.subclassSubclassMethod = function(){};
204-
205-
selectors = [
206-
"body",
207-
"html, body",
208-
"<div></div>"
209-
];
210-
211-
contexts = [undefined, document, jQueryDocument];
212-
213-
jQuery.expandedEach = jQuery.each;
214-
jQuery.each(selectors, function(i, selector){
215-
216-
jQuery.expandedEach({ // all methods that return a new jQuery instance
196+
SubSubclass = Subclass.sub(),
197+
jQueryDocument = jQuery( document ),
198+
contexts = [ undefined, document, jQueryDocument ],
199+
selectors = [ "body", "html,body", "<div></div>" ],
200+
methodArguments = {
217201
"eq": 1 ,
218202
"add": document,
219203
"end": undefined,
220204
"has": undefined,
221205
"closest": "div",
222206
"filter": document,
223207
"find": "div"
224-
}, function(method, arg){
225-
jQuery.each(contexts, function(i, context){
226-
227-
description = "(\""+selector+"\", "+context+")."+method+"("+(arg||"")+")";
228-
229-
deepEqual(
230-
(function(var_args){ return jQuery.fn[method].apply(jQuery(selector, context), arguments).subclassMethod; })(arg),
231-
undefined, "jQuery"+description+" doesn't have Subclass methods"
232-
);
233-
deepEqual(
234-
(function(var_args){ return jQuery.fn[method].apply(jQuery(selector, context), arguments).subclassSubclassMethod; })(arg),
235-
undefined, "jQuery"+description+" doesn't have SubclassSubclass methods"
236-
);
237-
deepEqual(
238-
Subclass(selector, context)[method](arg).subclassMethod, Subclass.fn.subclassMethod,
239-
"Subclass"+description+" has Subclass methods"
240-
);
241-
deepEqual(
242-
Subclass(selector, context)[method](arg).subclassSubclassMethod, undefined,
243-
"Subclass"+description+" doesn't have SubclassSubclass methods"
244-
);
245-
deepEqual(
246-
SubclassSubclass(selector, context)[method](arg).subclassMethod, Subclass.fn.subclassMethod,
247-
"SubclassSubclass"+description+" has Subclass methods"
248-
);
249-
deepEqual(
250-
SubclassSubclass(selector, context)[method](arg).subclassSubclassMethod, SubclassSubclass.fn.subclassSubclassMethod,
251-
"SubclassSubclass"+description+" has SubclassSubclass methods"
252-
);
208+
};
253209

210+
jQueryDocument.toString = function() {
211+
return "jQueryDocument";
212+
};
213+
214+
Subclass.fn.subMethod = function(){};
215+
SubSubclass.fn.subSubMethod = function(){};
216+
217+
jQuery.each( contexts, function( i, context ) {
218+
jQuery.each( selectors, function( i, selector ) {
219+
jQuery.each( methodArguments, function( method, arg ){
220+
221+
var description = "(\"" + selector + "\", " + context + ")." + method + "(" + (arg || "") + ")",
222+
$instance = jQuery( selector, context )[ method ]( arg ),
223+
$subInstance = Subclass( selector, context )[ method ]( arg ),
224+
$subSubInstance = SubSubclass( selector, context )[ method ]( arg );
225+
226+
// jQuery
227+
strictEqual( $instance.subMethod, undefined,
228+
"jQuery" + description + " doesn't have Subclass methods" );
229+
strictEqual( $instance.subSubMethod, undefined,
230+
"jQuery" + description + " doesn't have SubSubclass methods" );
231+
232+
// Subclass
233+
strictEqual( $subInstance.subMethod, Subclass.fn.subMethod,
234+
"Subclass" + description + " has Subclass methods" );
235+
strictEqual( $subInstance.subSubMethod, undefined,
236+
"Subclass" + description + " doesn't have SubSubclass methods" );
237+
238+
// SubSubclass
239+
strictEqual( $subSubInstance.subMethod, Subclass.fn.subMethod,
240+
"SubSubclass" + description + " has Subclass methods" );
241+
strictEqual( $subSubInstance.subSubMethod, SubSubclass.fn.subSubMethod,
242+
"SubSubclass" + description + " has SubSubclass methods" );
254243
});
255244
});
256245
});

0 commit comments

Comments
 (0)