0

I'm been having some trouble playing around with the backbone.js library recently - namely the variables that I instantiate (both an extended View and Model) remain undefined.

Here's some code to show you what I mean:

<script type="text/javascript">
$(document).ready(function(){

var img = new ImageUrl();
var search = new ImageSearchDialogue({el: '.imageUrl', model: img}).render();


});
</script>

Here's the script that I've placed in the HTML document - both variables are undefined however. (I've checked with Firebug)

Is this a scoping issue perhaps?

4
  • So I'm assuming that ImageUrl extends a Backbone.Model and the ImageSearchDialogue extends a Backbone.View? Also the Backbone and Jquery libraries have been included in the page? Commented Oct 30, 2012 at 21:46
  • Yep to the first question. The libraries have been included in this order: jquery, underscore, backbone. Commented Oct 30, 2012 at 22:36
  • I checked them out in a console window after running the code through a browser. (both firefox & chrome) - The variables 'img' and 'search' are both undefined. I'm able however to create new instances from both relevant Constructor functions in said console window. Commented Oct 30, 2012 at 23:35
  • This image may clears things up in my above comment: img.photobucket.com/albums/v282/Ace_of_Spades01/console.jpg Commented Oct 30, 2012 at 23:36

1 Answer 1

1

Is it that you’re expecting the variables to be defined globally? They’re currently only defined in the scope of the function. Try this:

window.img = new ImageUrl();
window.search = new ImageSearchDialogue({el: '.imageUrl', model: img}).render();
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm this could be the case. But I've noticed that there's another View object that I can instantiate that is visible in the global scope. var menu = new Menu({el: '#menu'}); This one however is not passed a model object argument.
As an aside, you can make variables global when defining in the scope of a function by not including a 'var'. e.g. img = new ImageUrl();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.