john-n-smith / mvcobject-js

An implementation of the KVO pattern, inspired by Google Maps API v3 MVCObject class
7 stars 1 forks source link

Clearing an observer by setting it to null throws an error next time the observed property changes. #4

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
var abc = new MVCObject;
abc.property = null;
abc.property_changed = function() { console.log('triggered'); };
abc.set('property', 'value1');
> triggered
abc.property_changed = null;
abc.set('property', 'value2');
> "TypeError: Property 'property_changed' of object #<MVCObject> is not a 
function" from line ~225

I expect no error when my callback is null. Instead I get an error that 
basically says the code tried to run "null".

Using latest version 0.21. By replacing the following line:

if(key + '_changed' in this) this[key + '_changed']();

with this:

if(typeof this[key + '_changed'] === 'function') this[key + '_changed']();

I was able to fix the error. Also verified that an actual observer function 
there will run.

Cheers,
Dave

Original issue reported on code.google.com by dcporter@gmail.com on 26 Jan 2013 at 1:07