dongpaocuan7498
2015-09-21 10:59
浏览 123
已采纳

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

Hi I am new to Backbone JS and was simply playing around and trying to learn. I am stuck with this for quite some time now. any help will be appreciated, Thanks in advance!

This is my Model

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

This is my collection

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

I want to add these data to my previous array

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 ));

Cant really add the next 2 array of data into my collection

图片转代码服务由CSDN问答提供 功能建议

您好我是Backbone JS的新手,只是在玩耍并试图学习。 我已经坚持了很长一段时间了。 任何帮助将不胜感激,提前感谢!

这是我的模型

  var Human = Backbone.Model.extend({\  n默认值:{
 name:'Fetus',
 age:0,
 child:'noname'
},
}); 
 
 
 
 var human = new Human();  
 human.set({name:“Thomas”,年龄:67岁,孩子:“Ryan”});  //正常工作
   
 
 

这是我的集合

  var Person = Backbone.Collection.extend({\  n model:Human 
}); 
 
 
var Human1 = new Human({name:“Khaleesi”,age:“37”,child:“Drogon”}); 
var Human2 = new Human({name  :“Rahul”,年龄:“25”,孩子:“Rita”}); 
var Human3 = new Human({name:“Seema”,年龄:“26”,孩子:“Maruti”}); 
 \  nvar thePeople = new Person([Human1,Human2,Human3]); 
document.write(“&lt; / br&gt;”); 
document.write(JSON.stringify(thePeople.models));  //这也可以正常工作
   
 
 

我想将这些数据添加到我以前的数组

  var sm =  this.Person.add(new Human([
 {姓名:“Hemath”,年龄:“32”,孩子:“sophie”},
 {姓名:“Siddharth”,年龄:“26”,孩子:“  Tom“} 
])); 
 
 
document.write(JSON.stringify(sm.models)); 
   
 
 

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumeng1143 2015-09-21 11:02
    已采纳

    You should add array to collection

    var sm = this.Person.add([
      { name: "Hemath", age: "32", child: "sophie" },
      { name: "Siddharth", age: "26", child: "Tom" }
    ]);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题