Json Backbone.js+;休息

Json Backbone.js+;休息,json,rest,backbone.js,Json,Rest,Backbone.js,我对Backbone.js非常陌生,一直在遵循一些教程来编写下面的脚本。我试图实现的是在使用路由时从RESTAPI检索JSON数据。如果你看朋友路线的人物功能,你可以看到我将如何处理这个问题。我哪里做错了 <!DOCTYPE html> <html> <head> <title>I have a back bone</title> </head> <body> <button id="add

我对Backbone.js非常陌生,一直在遵循一些教程来编写下面的脚本。我试图实现的是在使用路由时从RESTAPI检索JSON数据。如果你看朋友路线的人物功能,你可以看到我将如何处理这个问题。我哪里做错了

<!DOCTYPE html>
<html>
<head>
    <title>I have a back bone</title>
</head>
<body>
    <button id="add-friend">Add Friend</button>
    <ul id="friends-list">
    </ul>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
<script>
Friend = Backbone.Model.extend({
    name: null,
    age: null,
});
FriendDetailModel = Backbone.Model.extend();
FriendDetailCollection = Backbone.Collection.extend({
    url: 'slim/index.php/friends/',
    model: FriendDetailModel

});

Friends = Backbone.Collection.extend({
    initialize: function(models, options) {
        this.bind("add",options.view.addFriendLi);
    }
});
AppView = Backbone.View.extend({
    el: $("body"),
    initialize: function() {
        this.friends= new Friends(null, {view:this});
    },
    events: {
        "click #add-friend":  "showPrompt",
    },
    showPrompt: function () {
        var friend_name = prompt("Who is your friend?");
        var friend_age = prompt("What is your friends age?");
        var friend_model = new Friend({name: friend_name, age: friend_age});

        this.friends.add(friend_model);
    },
    addFriendLi: function(model) {
        $("#friends-list").append("<li>" + model.get('name') + " " + model.get('age') + "</li>");
    }
});
var appview = new AppView;

AppRouter = Backbone.Router.extend({
    routes: {
        "friends":"people",
        "person/:name":"personDetail"
    },

    people: function() {
        console.log('all the people');
        var people = new FriendDetailCollection;
            people.fetch({
                    success: function(data) {
                            console.log(data);
                    }

    },

    personDetail: function(name) {
        console.log('one person named ' + name);
    }
});

var approuter = new AppRouter;
Backbone.history.start();
</script>
</body>
</html>
如果我使用console.log(data.toJSON());它回来了

[]

我最终通过以下操作解决了问题:

我在路由器外创建了一个新集合:

var people = new FriendDetailCollection;
创建视图时,我指定了以前创建的集合

friendview = new FriendView({collection: people});
我的FriendView中有一个输入错误。以前我有uz.bind(这个“render”)。这是必须的

_.bindAll(this,'render');
此外,我将console.log放在FriendView中的render()函数中

FriendView = Backbone.View.extend({
        el: $("body"),
        initialize: function() {
                _.bindAll(this,'render');
                this.collection.bind('reset', this.render);
        },
        render: function() {
                console.log(this.collection.toJSON());
        }

});

你能把你的问题说得更具体一点吗?到底出了什么问题?(例如错误、无数据等)我添加了更多信息。谢谢你在这方面的帮助!
FriendView = Backbone.View.extend({
        el: $("body"),
        initialize: function() {
                _.bindAll(this,'render');
                this.collection.bind('reset', this.render);
        },
        render: function() {
                console.log(this.collection.toJSON());
        }

});