OK, the real-world use is this:
I'm writing a plugin for jquery that implements mvc pattern, and it
works like this:
Define a model:
$.mvc.model({
myModel: {
myMethod: function() {}
}
});
Define a controller and call a model from the controller:
$.mvc.controller({
myController: {
myAction: function() {
this.model('myModel').myMethod();
}
}
});
Here, the model & controller passed in by the user can't be stored
immediately, they need to extend instances constructed by Model &
Controller classes I defined somewhere else. So I need to iterate over
these objects' properties to modify them. For this snippet, there is
no need to wrap a function, so having $.map() works on objects won't
make a big difference, but consider this:
$.mvc.controller({
myController: {
myAction: function() {
// Pass in a callback function,
// once model has retrieved the data,
// it'll pass the data as the first parameter
// to the callback function
this.model('myModel').myMethod(function(data) {
// data returned
});
}
}
});
$.mvc.model({
myModel: {
myMethod: function() {
// model passes the data by returning it directly
return 'data';
}
},
myAnotherModel: {
myAnotherMethod: function() {
this.ajax({ url: './', success: function(data) {
// or by returning it in the ajax callback function
return data;
}})
}
}
});
This is when I need to modify user defined functions to support such
usage, and it comes the problem of scoping. It'll be very handy if
$.map() works on objects.
And I believe once an application gets more and more complicated,
it'll stand a good chance that some kind of hash-like objects need to
be modified.
Regards
On Oct 30, 9:38 pm, Scott Sauyet <[email protected]> wrote:
> On Fri, Oct 30, 2009 at 2:22 AM, gMinuses <[email protected]> wrote:
> > It's very detailed, thank you for the demos! But I don't quite
> > understand the purpose of them.
>
> I was trying desperately to come up with some real-world use of your
> suggestion, and noted that your example used callbacks, and I thought
> you might want to use the technique to instrument your callback
> methods in some manner.
>
> > If you are saying $.map() shouldn't
> > work on objects because wrapping functions is not a good practice,
> > then $.map() should also not work on arrays. Here is why:
>
> > // options now is a sequence
> > var options = [ function() { this[1]() }}, function() { alert
> > ('Oops') } ]
>
> I'd contend that it's very difficult to see a real-world use of $.map
> applied to functions.
>
> > $.map() works on this "object", and it also causes the "problem" you
> > have illustrated. Actually, I don't perceive it as a "problem". What
> > you are doing is counting how many times the callback functions have
> > been called. Since some callback functions will call another callback
> > function, when three of them get called, it makes sense these callback
> > functions have been called six times in total.
>
> But "helper()" was not intended as a callback function. It was an
> implementation detail. By accidentally mapping that function, we've
> changed the semantics in a way we wouldn't have done if we'd chosen to
> apply our transformation directly to the callbacks we cared about.
>
> > Maybe I'm too dumb to get the essence of your demos, could you explain
> > more to me? Thanks.
>
> It's pretty clear you're not too dumb. I'm just trying to figure out
> how you'd want to use such an extended method. The callbacks made me
> wonder if this was the sort of detail you had in mind.
>
> Cheers,
>
> -- Scott
--
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en.