1

I have jquery.js, backbone.js ,underscore.js inlcuded in my html header (I can see these files from browser)

 <script type='text/javascript'>
    (function($){
     var Student= Backbone.Model.extend({
          initialize: function(){
              console.log("studentis createrd");
          }
      });

        var students= Backbone.Collection.extend({
            model: Student
        });  

        console.log(students.models);
    })(jQuery);
</script>

I got this error message enter image description here

UPDATE:html header

<script src="/static/js/jquery.js" type="text/javascript"> </script>
<script src="/static/js/backbone.js" type="text/javascript"></script>
 <script src="/static/js/underscore.js" type="text/javascript"></script>
4
  • When are you importing the backbone file? Commented May 23, 2013 at 18:57
  • underscore needs to come before backbone Commented May 23, 2013 at 19:02
  • @Justin I have updated html header. Commented May 23, 2013 at 19:02
  • Backbone relies on underscore being loaded first. Commented May 23, 2013 at 19:40

3 Answers 3

7

First you need to make sure that Backbone is loaded properly --

The order should be

   ---> jQuery
   ---> underscore
   ---> backbone

Next need to create a new instance of the collection

You cannot operate directly on the Model or Collection before creating an instance of it.

(function ($) {
    var Student = Backbone.Model.extend({  // Created a Student model
        initialize: function () {
            console.log("studentis createrd");
        }
    });

    var Students = Backbone.Collection.extend({  // Create a Students collection
        model: Student
    });

    var student = new Student(); // Create a instance of Student model

    var students = new Students(); // New instance of Students collection
    students.add(student);   // Add a model to the collection

    console.log(students.models);
})(jQuery);

Check Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

I think you also need to pass backbone as parameter to you function along with jQuery

 <script type='text/javascript'>
    (function($,Backbone){
     var Student= Backbone.Model.extend({
         initialize: function(){
            console.log("studentis createrd");
         }
      });

      var students= Backbone.Collection.extend({
         model: Student
      });  
     console.log(students.models);
      })(jQuery,Backbone);
</script>

1 Comment

This isn't required because Backbone is declared in the global(ly accessible) scope
1

Per my comment:

When are you importing the backbone file?

Backbone has a dependency on underscore to work, so you need to have Jquery -> Underscore -> Backbone in order for backbone to load properly.

Comments

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.