微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决对于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 举报,一经查实,本站将立刻删除。