Alex B
Alex B

Reputation: 1019

Backbone.js Collection error

I have created a little autosuggest app with Backbone and it works in its own page (http://www.alexbarron.co.uk/bbExample). However when I integrate it into my website the this.model property of one of my collections is never being set. I have debugged it to this point and found that this is where the difference between the integrated version and the standalone version begins.

Script order on the 2 pages is the same.

To replicate:

  1. please visit: http://alexbarron.co.uk/projects/tree-categories-autosuggest/demos/backbone-js
  2. Type "bat"
  3. Select batman
  4. Check the console. this.model in the Collection constructor is never set. In my standalone version this gets set to:

function (){ return parent.apply(this, arguments); }

Any help would be appreciated.

Upvotes: 0

Views: 33

Answers (1)

user3525701
user3525701

Reputation: 28

Problem is the order of downloading collection.js and model.js. I put a breakpoint in collection.js and model.js at first line, so that when the files are interpreted, the breakpoints are hit. collection.js was downloaded first and model is set as undefined, as model.js containing TreeCategoriesAutoSuggest.Models.Category is not downloaded yet :

TreeCategoriesAutoSuggest.Collections.Suggestions = TreeCategoriesAutoSuggest.Collections.SelectedCategories = Backbone.Collection.extend({
    model: TreeCategoriesAutoSuggest.Models.Category
});

In index.html, change the order. Place model.js first and then collection.js as shown below.

<script type="text/javascript" src="/frontend/javascripts/projects/tree-categories-autosuggest/demos/backbone-js/models.js"></script>
<script type="text/javascript" src="/frontend/javascripts/projects/tree-categories-autosuggest/demos/backbone-js/collections.js"></script>

Upvotes: 1

Related Questions