调用Backbone集合的fetch函数时,“未定义不是函数”错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用Backbone集合的fetch函数时,“未定义不是函数”错误相关的知识,希望对你有一定的参考价值。
调用fetch
函数时出错。
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>
答案
有几个问题。我正在建立@ChrisG的答案。
- 您需要实例化一个集合。这是我的代码:
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();
- 我更新了Underscore和Backbone的版本 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
以上是关于调用Backbone集合的fetch函数时,“未定义不是函数”错误的主要内容,如果未能解决你的问题,请参考以下文章
backbone keydown提交表单而不是调用所需的函数
Backbone Collections - fetch(),成功回调无法正常工作