将一组集合添加到Backbone JS中以前添加的集合中

时间:2015-09-21 10:59:44

标签: javascript php backbone.js collections models

您好我是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 ));

无法将下两个数据数组添加到我的收藏中

1 个答案:

答案 0 :(得分:0)

你应该add数组到集合

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