我的html标题中包含了jquery.js,backbone.js,underscore.js(我可以从浏览器中看到这些文件)
<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>
我收到此错误消息
更新:html标题
<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>
答案 0 :(得分:7)
首先,您需要确保正确加载Backbone -
订单应该是
---> jQuery
---> underscore
---> backbone
接下来需要创建集合的新实例
在创建模型或集合的实例之前,您无法直接在模型或集合上操作。
(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 强>
答案 1 :(得分:1)
我认为你还需要将backbone作为参数传递给你的函数以及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>
答案 2 :(得分:1)
根据我的评论:
您何时导入骨干文件?
Backbone依赖于下划线来工作,所以你需要有Jquery - &gt;下划线 - &gt;骨干,以便骨干正确加载。