Saturday 9 June 2012

Stupid JS mistake on my part

I've started using knockout js for my declarative bindings and thought I'd a newbie javascript mistake on my part. I spend half an hour or so trying to figure out why my code that is loaded using jquery's .load() method is not working. According to the jQuery docs, the .load() method also loads any javascript sources as well. However, I found out (the hard way), that if there's a silly mistake like the following in the script that gets parsed, jQuery won't load the javascript, and will not report any errors.

The following code is what I had:


 function Page(viewModel) { 
   /** --- VARIABLES --- */
   var self = this;
   var self.viewModel = viewModel;
   /** --- METHODS --- */
   self.init = function() {
     $('.search-btn').on('click', function() {
       alert('hello');
     });
   };
   self.updateSearch = function() {
     viewModel.results.push('s');
   };
 }  

Can you spot the mistake?

It's here:


 var self.viewModel = viewModel;  

To fix it, make it so:


 var viewModel = viewModel;  



No comments:

Post a Comment