bind method with arguments to backbone model change event - javascript

I think this question is more a javascript question than a backbone question, but I've run into it while developing an application in backbone, so that's the context I will ask it in.
I am binding a method with arguments to a model's change event. The code below executes that method when the listener is bound, not when the event is fired:
this.model.on("change:disposition", this.dChange("disposition"), this);
while the following code executes the method when the change event is fired (the desired behavior):
this.model.on("change:disposition", function(){ this.dChange("disposition"); }, this);
I would appreciate it if someone could describe what specifically is happening in these two instances. Also, is there is a better way to bind a method with arguments rather than wrapping it in a closure as I've done?
Thanks.

When you call this.dChange("disposition") you're invoking the function. (You're using the parentheses () to invoke)
But when you do function() {} or this.dChange you're in fact referencing a function object. And it's this reference that the event manager will call once the event is fired.
Side note: In your case, instead of using an anonymus function, you could use the bind method of Underscore.js like this:
this.model.on("change:disposition", _.bind(this.dChange, this, "disposition"));

Related

model.on('change', function()) not working properly

I am using backboneJS model.on ('change:attribute',functionName(someParameter)) to listen to change in model's attribute and call a funcion with given parameter. But the problem I am facing is the function is being called initially even when there is no change in the model. After that,even when the model changes, the function is not called. I did some trials and found out that without the parameter, if I called ('change:attribute',functionName),
the events fired properly. I can not understand what the problem is. Can anyone help as I think I am missing something very basic here. And a way to approach such problem would be much appreciated. Thanks.
The .on() method expects you to pass the callback function or method that will be called to handle the event. But in your first example you tried to pass a result of that callback.
So inside it will execute yourCallback.call(...) or yourCallback.apply(...). Obviously it could not execute the .call() method of non-function value.
But you can wrap the method call in anonymous function though if you really need it. For example if you need to use that someParameter value:
var MyView = Backbone.View.extend({
// ...
myMethod: function(someParameter) {
this.model.on('change:attribute', function() {
functionName(someParameter);
});
}
});

AngularJS + Jasmine: spy an event callback

I have issues to spyOn a method bind to an event with scope.$on in a factory service, with Jasmine. The real method originally passed is called, instead of the spy.
I've made a plinkr: http://plnkr.co/edit/2RPwrw?p=preview
Thanks for your help.
This is because of the way you're binding the callback. Change
service.$on('hello', service.method);
to
service.$on('hello', function() {
service.method();
});
When you say spyOn(service, 'method'), you're saying "replace the value that is referenced at service.method with a spy." However, your original service.$on code doesn't look up the value at service.method when the event is triggered--instead it looks it up when the service is initialized. Thus, changing the reference that service.method points to later has no effect.

How is called a callback given to jQuery bind?

In this code, will the trigger then call topbar.resize or will it just simply run the function contained in it? I assume there is a context difference between the two, or how does that work?
jQuery.bind("hideBackstage", topbar.resize);
Edit: How do I make it just simply call topbar.resize?
The call will be equivalent to
topbar.resize.call(touchedElement, jqueryWrappedEvent);
That is the function "contained" in topbar.resize will be called with receiver (this) the element having received the event and with parameter the event (wrapped as a jquery event).
This is different from
topbar.resize()
because
the receiver (this) won't be topbar
an event is provided as parameter
EDIT following your EDIT :
If you just want the call to be equivalent to topbar.resize(), simply do this :
jQuery.bind("hideBackstage", function(){topbar.resize()});
you can use jQuery proxy or
jQuery.bind("hideBackstage", function(){
topbar.resize.apply(topbar, arguments); // seting context as topbar for resize.
});
You can use $.proxy() (which funnily enough is analogous to Function.prototype.bind()).
jQuery.bind("hideBackstage", $.proxy(topbar.resize, topbar));
However, you won't have access to the event object.

Yahoo YUI library CustomEvent class - subscribe() and fire()

I am fairly new to javascript and using the YUI yahoo library.
http://developer.yahoo.com/yui/docs/YAHOO.util.CustomEvent.html
I am trying to understand the subscribe and fire methods in a CustomEvent class. I have two questions regarding these
When the fire method is called. Does it fire all the functions that are subscribed to the event?
How do I fire an individual event? The subscribe method takes the parameter "obj An object to be passed along when the event fires." so is there
a way I can fire only this particular object?
Correct me if I misunderstand anything in the CustomEvent. :)
When the fire method is called. Does it fire all the functions that
are subscribed to the event?
The custom event (publisher) fires without caring who is listening (subscriber).
Calling the fire() method causes all the listener methods to get fired.
How do I fire an individual event?
You fire an individual event using :
var onCustomEvent = new YAHOO.util.CustomEvent('onCustomEvent');
onCustomEvent.fire();
The subscribe method takes the parameter "obj An object to be passed along when the event fires." so
is there a way I can fire only this particular object?
You can fire the customEvent not the object ,
passing objects is used to pass arguments so they can be accessed in the subscribe method :
onCustomEvent.fire({action:'fire'});
onCustomEvent.subscribe(method);
function method(event, arguments, obj) {
var action= args[0].action;//action contains fire
}
event returns the name of the custom event
arguments is the set of arguments that are passed in the fire event
obj is the argument that is passed in the subscribe method.
If this is a new project, you should use YUI3. Then you might benefit from this documentation.

Why wrapping into a function is required in .change() of jQuery?

Why this works in jQuery :
$('#selCars').change(function(){
alert( "I have changed!" );
})
but not this one :
$('#selCars').change(alert( "I have changed!" ) );
You pass a function reference to .change(). Your second example just has code there, not a function reference.
Your first example works because it passes a function reference which IS what is required.
A function reference is required because this is a callback that will be called at a later time. The .change() function which executes immediately needs to save the callback reference into it's own variable and then call it later when the change event actually occurs. To do that, it needs a function to call at that later time, not a raw piece of code.
And, the other answer is because, .change() was written to require a function reference. That's how the developers that spec'ed and wrote it designed it. If you want it to work, you have to follow their rules.
Because it's a callback, i.e. you're passing something that be called back later, so what you've to pass is a reference to a function, and that reference will be stored and called when the event will fire.
The change method doesn't store some code, it stores only a pointer to the function. Your function is called an event handler.
It's because .change() attaches an event handler to an element. The handler won't be called until the event has occurred.
Since in JavaScript, functions are just another datatype, you could also do this:
var handler = function(event) {
alert("I have changed!");
}
$('#selCars').change(handler);
Note that handler is a function, Whereas alert() would just return undefined.

Categories

Resources