对于backbone.js,如何在URL /people 中的数据库中创建模型?

问题描述

如何在 URL /people 中创建模型?我试过这个例子:

var John = New Person ({name: "john",age: "33"}); 
John.save();

这不会保存并弹出错误

未捕获的错误:必须指定“url”属性函数

当我输入姓名和年龄并点击新人时,它出错

[HTTP/1.1 405 方法不被允许。

所以我坚持创建模型以在 URL 中保存在数据库中并检索数据库中的记录。我该怎么做?

<!DOCTYPE html>
<head>
</head>
<body>
  <div id="peopleListScope">
    <ul id="allMyPeople">
    </ul>
    Name: <input type="text" name="name">
    Age: <input type="number" name="age">
    <button>New Person</button>
  </div>      
  <script src ="jquery-3.6.0.js"></script>
  <script src ="underscore-umd-min .js"></script>
  <script src ="backbone-min.js"></script>
  <script>
    var Person = Backbone.Model.extend({
      defaults: {
        name: null,age: null,}
    });
    var PeopleCollection = Backbone.Collection.extend({
      model: Person,url: "/people",//the root of the RESTful route
      parse: function(){
        //...we may or may not need to use parse!
      }
    });
    var peopleList = new PeopleCollection();
    peopleList.fetch();
    var PeopleCollectionView = Backbone.View.extend({
      el: "#peopleListScope",initialize: function(){
        this.listenTo(this.collection,'sync',this.render);
        this.listenTo(this.collection,'change',this.render);
      },render: function(){
        this.collection.each(function(person){
        //build the individual view for each model...
        })
      },events: {
        'click button':'createPerson'
      },createPerson: function(){
        $name = this.$('input[name="name"]');
        $age = this.$('input[name="age"]');
        this.collection.create({
            name: $name.val(),age: $age.val(),});
        $name.val('');
        $age.val('');
        }
    });
    var peopleListView = new PeopleCollectionView({collection: peopleList});
  </script>
</body>
</html>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)