2017-12-18 71 views
0

我在調用fetch函數時出現錯誤。調用Backbone集合的獲取函數時出現「未定義不是函數」錯誤

Undefined is not a function Error in Backbone js 

var models = Backbone.Model.extend({ 
 
    userId: function() { 
 
    return this.get('userId'); 
 
    }, 
 
    id: function() { 
 
    return this.get('id'); 
 
    }, 
 
    body: function() { 
 
    return this.get('body'); 
 
    } 
 
}); 
 
//Collections 
 
var todolistStore = Backbone.Collection.extend({ 
 
    url: function() { 
 
    return 'https://jsonplaceholder.typicode.com/posts' 
 
    }, 
 
    model: models, 
 
    parse: function(response) { 
 
    return response; 
 
    } 
 
}); 
 
todolistStore.fetch();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js" type="text/javascript"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>

+1

看起來你需要先實例:'VAR的MyStore =新todolistStore() ; myStore.fetch();' –

+0

@ChrisG仍然不能正常工作 –

+1

您正在使用舊版本的下劃線和主幹。當我使用當前版本時,錯誤消失。 –

回答

5

有幾個問題。我正在建立@ChrisG的答案。

  1. 您需要實例化一個集合。下面是我的代碼:

    var Model = Backbone.Model.extend({ 
        userId: function() { 
         return this.get('userId'); 
        }, 
        id: function() { 
         return this.get('id'); 
        }, 
        body: function() { 
         return this.get('body'); 
        } 
    }); 
    
    //Collections 
    var TodoListStore = Backbone.Collection.extend({ 
        url: function() { 
         return 'https://jsonplaceholder.typicode.com/posts' 
        }, 
        model: Model, 
        parse: function(response) { 
         return response; 
        } 
    }); 
    
    var todolistStore = new TodoListStore(); 
    todolistStore.fetch(); 
    
  2. 我更新下劃線的版本和骨幹

    https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js

相關問題