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.
Asked
Active
Viewed 181 times
0

NehaN
- 164
- 1
- 5
-
can u share a fiddle link?? – Shashank Oct 08 '13 at 05:45
1 Answers
2
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);
});
}
});

Eugene Naydenov
- 7,165
- 2
- 25
- 43
-
Thanks for the help. It was an obvious mistake on my part,now that I see it. – NehaN Oct 08 '13 at 06:47