Skip to content

Commit

Permalink
[#832] defaults being applied to definition called via function
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDowne committed Mar 1, 2012
1 parent 0631200 commit 5dcebb5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
26 changes: 12 additions & 14 deletions popcorn.js
Expand Up @@ -1378,7 +1378,7 @@
// Storing the plugin natives
var natives = options._natives = {},
compose = "",
defaults, originalOpts, manifestOpts, mergedSetupOpts;
originalOpts, manifestOpts;

Popcorn.extend( natives, setup );

Expand All @@ -1401,9 +1401,6 @@
args[ 1 ]._running && natives.end.apply( this, args );
}, natives._teardown );

// Check for previously set default options
defaults = this.options.defaults && this.options.defaults[ options._natives && options._natives.type ];

// default to an empty string if no effect exists
// split string into an array of effects
options.compose = options.compose && options.compose.split( " " ) || [];
Expand Down Expand Up @@ -1454,25 +1451,21 @@
};
}

// Merge with defaults if they exist, make sure per call is prioritized
mergedSetupOpts = defaults ? Popcorn.extend( {}, defaults, options ) :
options;

// Resolves 239, 241, 242
if ( !mergedSetupOpts.target ) {
if ( !options.target ) {

// Sometimes the manifest may be missing entirely
// or it has an options object that doesn't have a `target` property
manifestOpts = "options" in manifest && manifest.options;

mergedSetupOpts.target = manifestOpts && "target" in manifestOpts && manifestOpts.target;
options.target = manifestOpts && "target" in manifestOpts && manifestOpts.target;
}

// Trigger _setup method if exists
options._natives._setup && options._natives._setup.call( this, mergedSetupOpts );
options._natives._setup && options._natives._setup.call( this, options );

// Create new track event for this instance
Popcorn.addTrackEvent( this, Popcorn.extend( mergedSetupOpts, options ) );
Popcorn.addTrackEvent( this, Popcorn.extend( options, options ) );

// Future support for plugin event definitions
// for all of the native events
Expand All @@ -1494,8 +1487,13 @@
// Extend Popcorn.p with new named definition
// Assign new named definition
Popcorn.p[ name ] = plugin[ name ] = function( options ) {
return pluginFn.call( this, isfn ? definition.call( this, options ) : definition,
options );

// Merge with defaults if they exist, make sure per call is prioritized
var defaults = ( this.options.defaults && this.options.defaults[ name ] ) || {},
mergedSetupOpts = Popcorn.extend( {}, defaults, options );

return pluginFn.call( this, isfn ? definition.call( this, mergedSetupOpts ) : definition,
mergedSetupOpts );
};

// Push into the registry
Expand Down
21 changes: 19 additions & 2 deletions test/popcorn.unit.js
Expand Up @@ -1630,13 +1630,13 @@ test( "Manifest removal", function() {

test( "Configurable Defaults", function() {

var expects = 13,
var expects = 14,
count = 0,
p;

function plus() {
if ( ++count === expects ) {
[ "configurable", "multiconfig", "overridden" ].forEach(function( val ) {
[ "configurable", "multiconfig", "overridden", "funtionInitDefaults" ].forEach(function( val ) {
Popcorn.removePlugin( val );
delete Popcorn.manifest[ val ];
});
Expand Down Expand Up @@ -1723,6 +1723,17 @@ test( "Configurable Defaults", function() {
};
});

Popcorn.plugin( "funtionInitDefaults", function( options ) {

equal( options.defaultItem, "foo bar", "defaults work inside auto setup function" );
plus();

return {
start: Popcorn.nop,
end: Popcorn.nop
};
});

p = Popcorn( "#video", {
defaults: {
overridden: {
Expand All @@ -1731,6 +1742,12 @@ test( "Configurable Defaults", function() {
}
});

p.defaults( "funtionInitDefaults", {
defaultItem: "foo bar"
});

p.funtionInitDefaults({});

p.defaults( "configurable", {

// set a default element target id
Expand Down

0 comments on commit 5dcebb5

Please sign in to comment.