Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow components to fire events on their ancestors? #1099

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/Ractive/prototype/fire.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
var nameOnly = /^(?:\.?\.\/)*(.*)/;
var parents = /^((?:\.\.\/)*).*/;

export default function Ractive$fire ( eventName ) {
var args, i, len, subscribers = this._subs[ eventName ];
var args, i, len, subscribers, target, ev, upCount;

if ( !subscribers ) {
return;
}
upCount = eventName.match(parents)[1].split('/').length - 1;

if ( upCount > 0 ) {
target = this;
ev = eventName.match(nameOnly)[1];

for ( i=0; i<upCount; i+=1 ) {
if ( !!target ) target = target._parent;
}

if ( !!target && typeof target.fire === 'function' ) {
args = Array.prototype.slice.call(arguments, 1);
args[ 0 ].parent = target;
args[ 0 ].child = this;
args.unshift(ev); // make sure to pass along a non-ancestor event name
target.fire.apply( target, args );
}
} else {
subscribers = this._subs[ eventName ];

if ( !subscribers ) {
return;
}

args = Array.prototype.slice.call( arguments, 1 );
args = Array.prototype.slice.call( arguments, 1 );

for ( i=0, len=subscribers.length; i<len; i+=1 ) {
subscribers[i].apply( this, args );
for ( i=0, len=subscribers.length; i<len; i+=1 ) {
subscribers[i].apply( this, args );
}
}
}
39 changes: 39 additions & 0 deletions test/modules/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,45 @@ define([ 'ractive' ], function ( Ractive ) {
simulant.fire( ractive.findAll( 'button' )[1], 'click' );
});

test( 'events can be fired on ancestors when components are nested', function ( t ) {
var shouldntHappen = function ( event ) { t.ok( false ); };

var cmp = Ractive.extend({
template: '<button on-click="../childClicked" on-dblclick="meClicked">click me</button><button on-click="../../foo">no fire, please</button>'
});

var ractive = new Ractive({
components: { cmp: cmp },
el: fixture,
template: '<span><cmp /></span>'
});

ractive.on( 'childClicked', function ( event ) {
t.ok( true );
t.equal( event.original.type, 'click' );
t.equal( event.parent, ractive );
t.equal( event.child, ractive.findComponent('cmp') );
});

ractive.on( 'meClicked', shouldntHappen);
ractive.on( 'foo', shouldntHappen );

var btns = ractive.findComponent( 'cmp' );

btns.on( 'meClicked', function ( event ) {
// this should have gone through the non-ancestor code path and thus have no parent/child
t.ok( !!!event.parent );
t.ok( !!!event.child );
});

btns.on( 'childClicked', shouldntHappen );
btns.on( 'foo', shouldntHappen );

simulant.fire( btns.findAll( 'button' )[ 0 ], 'click' );
simulant.fire( btns.findAll( 'button' )[ 0 ], 'dblclick' );
simulant.fire( btns.findAll( 'button' )[ 1 ], 'click' );
});


};

Expand Down