在Backbone JS中为前面添加的集合添加一个集合数组


Adding an array of collections to the Previously added collection in Backbone JS

嗨,我是Backbone JS的新手,只是在玩,试图学习。我被这个问题困扰了很长一段时间。任何帮助将不胜感激,提前感谢!

这是我的模型

var Human = Backbone.Model.extend({
  defaults: {
    name: 'Fetus',
    age: 0,
    child:'noname'
  },
 });

 var human = new Human();
 human.set({ name: "Thomas", age: 67, child: "Ryan"}); //Works fine

这是我的收藏

var Person = Backbone.Collection.extend({
  model: Human
});

var Human1 = new Human({ name: "Khaleesi", age: "37", child: "Drogon" });
var Human2 = new Human({ name: "Rahul", age: "25", child: "Rita" });
var Human3 = new Human({ name: "Seema", age: "26", child: "Maruti" });
var thePeople = new Person([ Human1, Human2, Human3]);
document.write("</br>");
document.write(JSON.stringify( thePeople.models )); // This also works fine

我想把这些数据添加到之前的数组

var sm = this.Person.add(new Human([
  { name: "Hemath", age: "32", child: "sophie" },
  { name: "Siddharth", age: "26", child: "Tom" }
]));
document.write(JSON.stringify( sm.models ));

你应该添加数组到集合

var sm = this.Person.add([
  { name: "Hemath", age: "32", child: "sophie" },
  { name: "Siddharth", age: "26", child: "Tom" }
]);