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

Modules: Proposal to add convenience to event callbacks #13

Closed
MarcDiethelm opened this issue May 9, 2012 · 1 comment
Closed

Modules: Proposal to add convenience to event callbacks #13

MarcDiethelm opened this issue May 9, 2012 · 1 comment
Assignees
Labels

Comments

@MarcDiethelm
Copy link
Contributor

Note: this is about the methods beforeBinding, onBinding and afterBinding.

During daily use of Terrific I often found it somewhat tedious to have to write some patterns over and over again. I'm talking about the following:
var self = this and $(selector, this.$ctx)
The first is needed to access the module instance inside closures (callbacks) that have a different scope than the surrounding method. The second pattern is used for efficiency in order to query only the module contents instead of the whole document.

I'm proposing changing the signature of those methods like this:

beforeBinding: function(self, $$, callback) {
    callback();
},

onBinding: function(self, $$) {
    // Examples...
    var $nav = $$('nav'); // shortcut for $('nav').find('nav') OR $('nav', this.$ctx);

    $nav.find('li').each(function() {
        // In here 'this' is a li. Without 'self' we have no reference to the Module (unless we stored it in a var).
        // (I don't like 'that').
        self.doSomethingWithNavLi(this);
    });
},

doSomethingWithNavLi: function(li) {
    // doing something
    return true;
},

afterBinding: function(self, $$) {}

I'm aware that this disturbs the beauty of the API, eg. when using JS skins , but in my experience the convenience of not having to repeat my self all the time trumps the aesthetic consideration.
About self... It's certainly not beautiful to pass the Module instance into the method as a param, but it makes the subsequent coding inside jQuery closures hassle-free. I chose self because it reminds me of the way we can reference a static class in PHP, and because I never liked that. But If you dont want to implement this I won't cry. :)
The second param $$ is more important to me... It would just be so handy to have this. We have to select some child of a module so often, that I think this really makes sense.

I'll say again that this is a very pragmatic proposal, more geared towards daily use than aesthetics. But I really think it has some merit! (And so do my co-workers.) I have the necessary changes to Tc.module.js in a local branch that I can push if you like...

@brunschgi
Copy link
Owner

HI Marc

I really thought long about your proposal above. Although I agree that it would add convenience for some of us, the disturbing of the beauty of the API weights heavier – at least in my opinion. Especially when it comes to Skins and the call of parent methods.

Due to this, I tried to solve your proposal by using a form of currying that does disturb the API. Unfortunately without success – at least so far :-(

Therefore I suggest to postpone this task in favor of aesthetics and implement it as soon we come up with a 'less immersive' solution.

What do you think?

btw: your explanation above has lead me to change that to self in the core and the examples ;-)

@ghost ghost assigned brunschgi Sep 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants