<!DOCTYPE HTML>
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
</head>
<body>
<script>
var mod=Backbone.Model.extend({});
var col=Backbone.Collection.extend({model:mod, url: 'test.php'});
col.fetch();
</script>
</body>
</html>
如果在Firefox中运行此原始代码,Firebug会给我以下错误:
TypeError: col.fetch is not a function
col.fetch();
为什么?我看不到错误或错字......谢谢。
答案 0 :(得分:0)
您的代码中几乎没有错误:
您需要指定模型和集合:
var Mod = Backbone.Model.extend({});
var Col = Backbone.Collection.extend({model: Mod, url: 'test.php'});
使用指定的模型创建集合实例:
var collectionInstance = new Col({model: new Mod()});
获取集合:
collectionInstance.fetch();
有关详细信息,请参阅Backbone docs。
答案 1 :(得分:0)
如果你不熟悉Class / Constructor / Prototype vs Instance,这里有一个解释:
假设我们有一个论坛,每个用户都有一些帖子。我们希望代表用户在名为&#34; Posts&#34;的集合中拥有的帖子。现在就是这样,你可以拥有多个用户,因此你需要多个instances
集合(基本上是一个具有Backbone提供的超酷功能的数组,你指定)。我们想要创建一个&#34;蓝图&#34;关于这个数组如何工作以及它可以做的很酷的事情(因为我们不想为我们创建的每个数组重写功能)。 Class / Constructor / Prototype就是那个蓝图。
//Post is a Model Class/Constructor/Prototype. We can make multiple "instances" of Post.
var Post = Backbone.Model.extend({
starPost: function() {
//contains code to "star" a post
},
pinPost: function(){
//contains code to "pin" the post
},
//some other cool functions to do cool things to a post
});
//Posts is the Collection Class/Constructor/Prototype. We can make multiple "instances" of Posts for each User.
var Posts = Backbone.Collection.extend({
url: function() {
return '/api/posts/' + this.options.userID
},
model: Post,
findStarredPosts: function(){
//has code to return all Post models that are "starred"
},
findPinnedPosts: function(){
//has code to return all Post models that are "pinned"
}
});
//please note that the FIRST ARGUMENT is an empty array (meaning we are starting this collection with no models inside). the SECOND ARGUMENT is the options (which will be saved into the instances `this.options`). `options` is what makes this instance unique when going to the server. You need to write in your `url` function HOW you're going to use what you passed in `options` to communicate with the server
var user1Posts = new Posts([], {
userID: 123 //this is an option passed in which is specific to this INSTANCE. It will get read by the `url` function and generate an instance of this posts collection Specific to user1
});
var user2Posts = new Posts([], {
userID: 456 //this is an option passed in which is specific to this INSTANCE. It will get read by the `url` function and generate an instance of this posts collection Specific to user2
});
//this command actually tells Backbone to go our and request user1's posts from the server and put them into the `user1Posts` collection.
user1Posts.fetch();
重点是,你永远不要在Constructors / Prototypes / Classes上运行函数。你只对他们的实例采取行动。您唯一能做的就是创建那些Constructors / Prototypes / Classes的实例。
我希望这有一定道理。如果不是,我可以(并将会)澄清。