NehaN
NehaN

Reputation: 164

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.

Upvotes: 0

Views: 200

Answers (1)

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

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);
        });
    }
});

Upvotes: 2

Related Questions