How to know whether a method is a call made by me thgh code or its from observer

70 Views Asked by At

I have a method in a view just like following.

testMethod : function() {
   //run code
}.observes('property1')

This method can either be trigerred directly by calling or triggered by the property1 observer. Is it possible to know inside the method, which way the call is getting triggered. Thanks

2

There are 2 best solutions below

0
On

I have stumbled upon this myself and have found no way to do so. I ended up doing something like this:

testMethod : function() {
   //run code
},
propertyObserver : function(){
   this.testMethod();
}.observes('property1')
0
On

When observer is called, it receives 2 arguments: the controller object, and the observed property which has changed and triggered the observer.

So you can check it like this:

testMethod : function() {
    if(arguments.length === 2 && arguments[1] === 'property1'){
        // you're triggered by property observer
    } else {
        // triggered directly
    }
}.observes('property1')

This, of course, can be spoofed by caller..