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 the child's instance as an argument #9

Merged

Conversation

frostbane
Copy link
Contributor

@frostbane frostbane commented Jun 8, 2016

Most of the time I need the ractive instace or the required child.

My parent's handler would look like

var onChildEvent = function(e){
    //get the child
    var childRactive = thisRactive.findChildren("id", "childID");

    //do something with the child
    childRactive.get(....);

    ...   
};

This became quite a pain since I have a very complicated widget with lots of binded events.

With this patch I could ease my work a bit since I don't have to manage the elements id and have to call findChildren (unless I really need to find my lost child)

The new and improved code would be

var onChildEvent = function(r, e){
    //no need to track ids anymore
    var childRactive = r;
    childRactive.get(....);

    ...   
};

add the child's instance as an argument to the parent's handler
@XavierBoubert
Copy link
Member

Thanks again @frostbane for you contribution!

I can see 2 ways to do that:

  • Your solution
  • Or by passing the child as this:
var onChildEvent = function(name, e) {
    //no need to track ids anymore
    var childRactive = this;
    childRactive.get(....);
    ...
};

(In require.js code:)

function _fireBindedEvent(parent, ractive, databinding) {
      var fireevent = function(event, name) {
        ractive.on(event, function() {
        Array.prototype.unshift.call(arguments, name);

          // parent.fire.apply(parent, arguments);
          parent.fire.apply(this, arguments);
        });

What's your preference?

@frostbane
Copy link
Contributor Author

this could mean a ton of things inside javascript

having this inside a handler would make the code less readable.

I prefer passing it as a separate argument to provide more clarity.

@XavierBoubert XavierBoubert merged commit 5c15560 into CodeCorico:master Jun 8, 2016
@frostbane
Copy link
Contributor Author

Thanks, I'll delete it later since I'm still at work.

@XavierBoubert
Copy link
Member

It's good for me. Published in 0.6.2.

@frostbane frostbane deleted the feature/add-childractive-argument branch June 8, 2016 11:26
# 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