Backbone.js Collection.fetch给出错误

时间:2012-05-31 17:39:12

标签: backbone.js backbone.js-collections

当我尝试调用collections.fetch方法时,我在控制台中收到以下错误:

Uncaught TypeError: Object function (a){return new j(a)} has no method 'isObject' 

这是我的app.js:

 var Items = Backbone.Collection.extend({
    url : function() {
        return "names/all/" + this.pageStart + "-" + this.pageEnd;
    },
    pageStart : 1,
    pageEnd : 5
});

var ScrollView = Backbone.View.extend({
    el : $('#list'),
    initialize : function() {
        _.bindAll(this, 'getNames');
        this.collection.bind('all', this.addAll);
        this.collection.fetch(); //getting error coz of this line here.
    },
    getNames : function() {
        this.collection.fetch();
    },

    addAll : function() {
        alert(JSON.stringify(this.collection));
        this.collection.each(function(item) {
            alert(":");
            this.addOne(item);
        });
    },
    addOne : function(item) {
        alert(item);
    }
});
var coll = new Items();
var scrollView = new ScrollView({collection: coll});

以下是我正在使用的脚本版本

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>

然而,我从服务器获得的响应是​​OK 200状态 这是我得到的用途

[{"firstName":"A","lastName":"last","id":"1"},{"firstName":"B","lastName":"last","id":"2"},{"firstName":"C","lastName":"last","id":"3"},{"firstName":"D","lastName":"last","id":"4"},{"firstName":"E","lastName":"last","id":"5"}]

最后这些是我的回复标题:

Request URL:http://localhost:8080/mongodb/names/all/1-5
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Referer:http://localhost:8080/mongodb/
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
X-Requested-With:XMLHttpRequest
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Thu, 31 May 2012 17:32:10 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

请帮助别人。 提前谢谢。

1 个答案:

答案 0 :(得分:4)

你使用下划线1.1.6和主干0.9.2,它需要下划线1.3.1。

请参阅http://backbonejs.org/#upgrading

  

升级到0.9

     

如果您正在升级,请确保同时升级您的版本   Underscore.js到最新版本 - 1.3.1或更高版本。