Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(require):add event listening #10

Closed

Conversation

frostbane
Copy link
Contributor

This patch lets the required child listen to an event fired by the parent.

This is quite long and I am writing this while on my job (secretly) so my words could be jumbled. child, widget, feature, template all mean the same thing.

Syntax

The syntax is quite confusing until you get used to it

<!-- parent.html -->
<rv-require name="widget"
            data-listen-☆☆="△△"></rv-require>

☆ is the event fired inside the widget

△ is the event fired by the parent

One example is:

I have a html form

<!-- parent.html -->
<rv-require name="advancedWidget"
            data-title="show advanced settings.."
            data-listen-cleanupadvanced="reset-form"></rv-require>
<rv-require name="superAdvancedWidget"
            data-title="show super advanced settings.."
            data-listen-cleanupsuperadvanced="reset-form"></rv-require>
<input type="reset"
       value="reset everything"
       on-click="reset-button" />

Now when I hit the reset button I would do some clean up and and also need to tell all the required widgets to do their own clean up.

//parent.js

//fired when the reset button is clicked
parentRactive.on("reset-button", function(e){
    //clean up the form

    var args = { /*some argument */ };

    //broadcast it to all the required children
    parentRactive.fire("reset-form", args);
});

And then all the listening children could respond to it accordingly.

//advancedWidget.js

advancedWidgetRactive.on("cleanupadvanced", 
    function(parentEventName, eventArgs){
        //in this example the handler arguments would be
        //parenEventName = "reset-form"
        //eventArgs      = { /*some argument */}

        //do some cleaning inside the widget
    });
//superAdvancedWidget.js

superAdvancedWidgetRactive.on("cleanupsuperadvanced", 
    function(parentEventName, eventArgs){
        //in this example the handler arguments would be
        //parenEventName = "reset-form"
        //eventArgs      = { /*some argument */}

        //do some cleaning inside the widget
    });

The main point is I could fire a single "reset-form" event inside the parent and all the required children would just listen to it, and respectively do their own clean up.

Another way to call it is broadcast or propagate an event to all children.

make the required child listen to events fired by the requiring parent
@frostbane
Copy link
Contributor Author

@XavierBoubert

I just added this today, it could still have some bugs.

Please give me until friday to test it.

@XavierBoubert
Copy link
Member

Sure

@frostbane
Copy link
Contributor Author

@XavierBoubert

There seems to be no problems.

You can merge it anytime. d=(^o^)=b

@XavierBoubert
Copy link
Member

Nice. I take a look this weekend and I merge it ;-)

var listenEvent = databinding.listening;

var fireLocalEvent = function(event){
parent.on(listenEvent[event], function(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's

parent.on(event, function() {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's ok, my bad.

@XavierBoubert
Copy link
Member

The syntax is quite confusing until you get used to it

Exactly lol.

@XavierBoubert XavierBoubert self-assigned this Jun 13, 2016
@XavierBoubert
Copy link
Member

Merged and released in 0.6.3 ;)

@frostbane frostbane deleted the feature/listen-to-parent-event branch June 15, 2016 02:55
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants