简体   繁体   English

骨干模型.save()覆盖先前的模型

[英]Backbone model.save() overwrite previous model

I have a problem when i save my model. 保存模型时出现问题。 Instead to create a new model with new cdi i always get the same cd1 and seems my model are just overwrite the previous. 而是用新的cdi创建一个新模型,我总是得到相同的cd1,看来我的模型只是覆盖了以前的模型。

Here is my simple sign up form: 这是我的简单注册表格:

Html HTML

<form class="form-signin">
      <h2 class="form-signin-heading">Please sign in</h2>
      <label for="inputName" class="sr-only">Name</label>
      <input type="text" id="inputName" class="form-control" placeholder="Name" required autofocus>
      <label for="inputSurname" class="sr-only">Surname</label>
      <input type="text" id="inputSurname" class="form-control" placeholder="Surname" required autofocus>
      <label for="inputNickname" class="sr-only">Nickname</label>
      <input type="text" id="inputNickname" class="form-control" placeholder="Nickname" required autofocus>
      <label for="inputEmail" class="sr-only">Email address</label>
      <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
      <label for="inputPassword" class="sr-only">Password</label>
      <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
      <div class="checkbox">
        <label>
          <input type="checkbox" value="remember-me"> Remember me
        </label>
      </div>
      <span id="btn-submit" class="btn btn-lg btn-primary btn-block" type="submit"> Sign in </span>
    </form>

Model: 模型:

var User = Backbone.Model.extend({
defaults: {
    inputName: "",
    inputSurname: "",
    inputNickname: "",
    inputEmail: "",
    inputPassword: "",
    rememberMe: false
},
url: 'src/users.json'

}); });

View: 视图:

var SignupView = Backbone.View.extend({

events: {
    'keypress input': 'getInputs',
    'click #btn-submit': 'saveInputs'
},

initialize: function() {
    // Make sure functions are called in the right scope
    _.bindAll(this, 'saveInputs');

    // Listen to model changes
    // this.model.bind('change', this.edit)
},

getUser: function(cid) {
    return this.model
},

saveInputs: function(evt) {
    evt.preventDefault();

    var model = this.model;

    this.$el.find('input[id]').each(function() {
        model.set( this.id, this.value );
    });
    this.model.save();
    console.log(model);

    this.$el.find('input').each(function() {
        this.value = ""
    });
}

}); });

Collection: 采集:

   var Searchers = Backbone.Collection.extend({
    model: User
});

And my app.js 还有我的app.js

 var SignUp = Backbone.View.extend({
    el: '.container'
});

var userModel = new User();
var searchersCollection = new Searchers();
searchersCollection.bind('change', function(rec){
    console.log('A record was changed:' + rec);
});


var signupView = new SignupView({
    el: $(".form-signin"),
    model: userModel
});

var signup = new SignUp();

So if a user type his data in the form and then i chekc in the web console: 因此,如果用户在表单中键入他的数据,然后我在Web控制台中点击:

userModel userModel

child {cid: "c1", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…} 子项{cid:“ c1”,属性:对象,_changing:否,_previousAttributes:对象,已更改:对象…}

but if new user type in new data and i check again web console: 但是,如果新用户输入新数据,然后再次检查Web控制台:

userModel userModel

child {cid: "c1", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…} 子项{cid:“ c1”,属性:对象,_changing:否,_previousAttributes:对象,已更改:对象…}

I got the same cid, i have just one model in my userModel and attributes are the last introduced. 我得到了相同的CID,我的userModel中只有一个模型,而属性是最后一个引入的模型。 What i need instead is to save all my users, not ovewrite them, and be able to access to them.. throught my model i guess? 我需要的是保存我的所有用户,而不是覆盖他们,并能够访问他们。通过我的模型吗?

I can't understand where i'm wrong :/ 我不明白我错了:/

cid means ClientID and is assigned when model object is created. cid表示ClientID,在创建模型对象时分配。 U should use "idAttribute" to define the remote id field. U应使用“ idAttribute”定义远程ID字段。

From what I understood what you need is a collection, not a model. 据我了解,您需要的是集合,而不是模型。

A model stands for a single entity, like a book, or a user. 模型代表单个实体,例如书或用户。 The save method of a model is for syncing it's data with the persistance layer and updating itself with the data return from it 模型的save方法是将其数据与持久层同步,并使用从其返回的数据进行更新

A collection is a collection of models. 集合是模型的集合。 If you want a new model with different cid everytime the user saves the form, you should add it to a collection, backbone will initialize a new instance of the model specified in collection's model property and add it to the collection. 如果您希望每次用户保存表单时都使用不同的cid创建新模型,则应addadd到集合中,主干将初始化在集合的model属性中指定的model的新实例,并将其添加到集合中。

Since you have an array of user entities, you should be pointing a collection to users.json , not a model. 由于您具有一组用户实体,因此应将一个集合指向users.json ,而不是模型。

var Searchers = Backbone.Collection.extend({
    url: 'src/users.json',
    model: User
});

And instead of a single model, you should pass a collection for that type of models: 而且,您应该传递该类型的模型的集合,而不是单个模型:

var signupView = new SignupView({
  el: $(".form-signin"),
  collection: searchersCollection
});

And in the save method pass the data that should be added as a new model to collections add method: 然后在save方法中将应作为新模型添加的数据传递给collections add方法:

saveInputs: function(evt) {
  evt.preventDefault();
  this.collection.add({
    name: "",
    age: ""
  });
}

Also note that you need to call the fetch method so that the collection will be populated from the endpoint. 还要注意,您需要调用fetch方法,以便从端点填充集合。

I suggest going through the documentation and tutorials since it seems you still needs to understand the basics. 我建议您仔细阅读文档和教程,因为您似乎仍然需要了解基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM