簡體   English   中英

集合上的.fetch()給出TypeError

[英].fetch() on collection gives TypeError

<!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();

這是為什么? 我看不到任何錯誤或錯字...謝謝。

您的代碼中幾乎沒有錯誤:

  1. 您需要指定模型和集合:

     var Mod = Backbone.Model.extend({}); var Col = Backbone.Collection.extend({model: Mod, url: 'test.php'}); 
  2. 使用指定的模型創建集合的實例:

     var collectionInstance = new Col({model: new Mod()}); 
  3. 獲取集合:

     collectionInstance.fetch(); 

有關更多詳細信息,請參閱Backbone docs

如果您不熟悉類/構造函數/原型與實例,請按以下說明進行:

假設我們有一個論壇,每個用戶都有一些帖子。 我們要表示用戶在稱為“帖子”的集合中的帖子。 現在,您可以擁有多個用戶,因此您將需要集合的多個instances (基本上是一個具有Backbone提供並指定的超酷功能的數組)。 我們想要為該數組的工作方式及其可以完成的工作創建一個“藍圖”(因為我們不想為所創建的每個數組重寫功能)。 一個類/構造函數/原型就是那個藍圖。

//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(); 

要點是,您永遠不要在構造函數/原型/類上運行函數。 您只能對他們的實例采取行動。 您唯一可以做的就是創建這些構造函數/原型/類的實例。

我希望這是有道理的。 如果沒有,我可以(並將)澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM