简体   繁体   English

遍历ribs.js中的集合

[英]iterating over collection in backbone.js

var User = Backbone.Model.extend({
  defaults : { 
    "students" : [ 
    {   
      name : "abc",
      grade: "A",
      school: "xyz"
     }], 
     "interest":
     {   
       music : "yes"
     }   
    }   
});

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

var obj = new UserList({"students" : [{name:"Rax", grade: "B", school : "javiers"}, {name:"John", grade: "C", school : "loreto"}], "interest" : {music: "yes"}});

for(var i = 0; i < obj.students.length; i++) {
  console.log(obj.students.at(i).get("name"));
  console.log(obj.students.at(i).get("grade"));
  console.log(obj.students.at(i).get("school"));
}

//i want to iterate over each students detail(name, grade and school) ..but this is giving me error " obj.students is undefined " ..why this error is coming?

First of all, there are several syntax errors, so look into console and fix these. 首先,存在几个语法错误,因此请查看控制台并进行修复。 Also, something is conceptually wrong with the object you pass to collection. 而且,从概念上讲,传递给集合的对象有问题。

Collection is a "user list", the model is one user. 集合是一个“用户列表”,模型是一个用户。 So, perhaps, you should change the defaults data description. 因此,也许您应该更改defaults数据描述。 It doesn't make sense to make students an array for a user. 使students成为用户的数组没有任何意义。

Also, when you create an instance of Collection, you then create a model by using create method , and not by just passing in the hash. 同样,当您创建Collection的实例时,您可以使用create 方法创建一个模型,而不仅仅是传递哈希值。 You could instatinate the Model that way, but not the Collection. 您可以通过这种方式设置模型,但不能设置集合。

So when you want to create a user, you pass only one "student" in, perhaps something like this (you also have to put "url" key in your Collection, otherwise it won't work): 因此,当您想创建一个用户时,您只可以传入一个“学生”,也许像这样(您还必须在“收藏夹”中放入“ url”键,否则它将无法工作):

var User = Backbone.Model.extend({
    defaults : { 
      "students" : [{   
        name : "abc",
        grade: "A",
        school: "xyz"
       }], 
       "interest":{   
         music : "yes"
       }   
    }
  });

  var UserList = Backbone.Collection.extend({  
    model : User,
    // `url` is required, otherwise Backbone will throw an error
    url : '/something'
  });
  var userList = new UserList();

  var bob = userList.create({
    students : [ { 
      name:'bob', 
      grade : 'a', 
      school : '123'
     } ],
    interest : { music : 'no' }
  });

  console.log( bob.get('students')[0].grade );

http://jsbin.com/elusey – here, hit "edit" and take a look at the code and in the console. http://jsbin.com/elusey –在此处,单击“编辑”,然后在控制台中查看代码。 You should see "a" as an output. 您应该看到“ a”作为输出。

You could actually simplify the model to this (as I told, the data structure described above doesn't really make sense for a single user): 您实际上可以简化此模型(正如我所告诉的,上述数据结构对于单个用户而言实际上没有任何意义):

var User = Backbone.Model.extend({
    defaults : {   
        name : "abc",
        grade : "A",
        school : "xyz"
        interests : ["music"]
    }
});

UPD . UPD Actually, you don't have to use create collection method in order to add models to your collection. 实际上,您不必使用create collection方法即可将模型添加到您的集合中。 When you create an instance of your collection you can pass in an array of models, something like this (note the difference, in your code you pass an object to a collection, but you actually need to pass an array, even if with one object in it): 创建集合实例时,您可以传递模型数组,如下所示(请注意,区别在于,在代码中,您将对象传递给集合,但实际上您需要传递数组,即使有一个对象在里面):

var userList = new UserList([{
    name:'bob', 
    grade : 'A', 
    school : '123',
    interests : [ "music" ]
  },{
    name:'george', 
    grade : 'B', 
    school : '321',
    interests : [ "drugs" ]
  }]);

You can find the working example that is using this way of adding models (don't forget to look into console) here: http://jsbin.com/elusey/2/edit 您可以在此处找到使用这种添加模型的方式的工作示例(不要忘记查看控制台): http : //jsbin.com/elusey/2/edit

Are you sure a user 'has many' students? 您确定用户“有很多”学生吗? That doesn't make much sense. 那没有多大意义。 However, if that is the case, you should add a definition for a student model and collection of students. 但是,如果是这种情况,则应为学生模型和学生集合添加定义。 Than, your user model should contain a student collection, and some logic in the set() method to transform an array of students to a students collection. 然后,您的用户模型应包含一个学生集合,以及set()方法中的一些逻辑,以将一组学生数组转换为一个学生集合。 If you want to handle collections/models nested under a model more gracefully, take a look at Backbone-relational . 如果要更优雅地处理嵌套在模型下的集合/模型,请查看Backbone-relational

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

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