Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干-无法在集合中循环_Javascript_Backbone.js_Underscore.js - Fatal编程技术网

Javascript 主干-无法在集合中循环

Javascript 主干-无法在集合中循环,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,我在循环使用数据填充视图的集合时遇到问题。当我尝试循环遍历集合并得到以下错误时 未捕获的TypeError:对象#没有方法“each” 我完全不知道为什么会出现这个错误,除了对象显然没有这个方法,但是我只有在运行drop函数时才会出现这个错误,请参见下面的代码 这里是主干代码 GroupListView.prototype.keyup = function() { this.filtered = this.collection.searchName(this.$el.find("#s

我在循环使用数据填充视图的集合时遇到问题。当我尝试循环遍历集合并得到以下错误时

未捕获的TypeError:对象#没有方法“each”

我完全不知道为什么会出现这个错误,除了对象显然没有这个方法,但是我只有在运行drop函数时才会出现这个错误,请参见下面的代码

这里是主干代码

GroupListView.prototype.keyup = function() {
      this.filtered = this.collection.searchName(this.$el.find("#search-query").val());
      return this.renderList(this.filtered);
};

GroupListView.prototype.renderList = function(collection) {
  var $collection, $this;
  console.log(this.$el);
  console.log(collection);
  if (!collection) {
    collection = this.collection;
    console.log(collection);
  }
  this.$el.find(".list-items").empty();
  $this = this.$el.find("#people-groups-list");
  $collection = collection;
  if ($collection.length < 1) {
    this.$el.find("#people-groups-list").hide();
    return this.$el.find("#people-groups-list").after('<div class="activity-no-show">\
    <p>To add a new group, click the + in the top right hand corner to get started.</p>\
</div>');
  } else {
     this.$el.find("#people-groups-list").show();

    collection.each(function(item) {
      //console.log(item);
      var displayView;
      displayView = new app.GroupListDisplayView({
        model: item,
        collection: $collection
      });
      return $this.append(displayView.render());
    });
    return this;
  }
};

GroupListDisplayView.prototype.render = function() {
      var $body;
      //console.log(this.model.toJSON());
      this.$el.html(this.template({
        m: this.model.toJSON()
      }));
      $body = this.$el.find(".card-body");
      $.each(this.model.get("people"), function(i, person) {
        var personTile;
        this.person = new app.Person({
          id: person.id,
          avatar: person.avatar,
          first_name: person.first_name,
          last_name: person.last_name
        });
        console.log(this.person);
        personTile = new app.PersonTileView({
          model: this.person
        });
        return $body.html(personTile.render());
      });
      return this.$el.attr("id", "group-card-" + this.model.id);
    };

GroupListDisplayView.prototype.drop = function(e) {
      var $collection, $model, person_id, request;
      e.preventDefault();
      $collection = this.collection;
      person_id = e.originalEvent.dataTransfer.getData('Text');
      request = new app.PersonGroupAdd;
      $model = this.model;
      return request.save({
        async: true,
        wait: true,
        person_id: person_id,
        group_id: this.model.get("id")
      }, {
        success: function(d) {
          return $model.fetch({
            async: true,
            wait: true
          });
        }
      });
    };

GroupListView.prototype.initialize = function() {
      this.collection.on("change", this.renderList, this);
      this.collection.on("reset", this.render, this);
      return this.renderList();
    };
GroupListView.prototype.keyup=function(){
this.filtered=this.collection.searchName(this.el.find(“#搜索查询”).val());
返回this.renderList(this.filtered);
};
GroupListView.prototype.renderList=函数(集合){
var$collection,$this;
console.log(这个$el);
控制台日志(收集);
如果(!集合){
collection=this.collection;
控制台日志(收集);
}
此.el.find(“.list items”).empty();
$this=this.$el.find(“#人员组列表”);
$collection=collection;
如果($collection.length<1){
这是.el.find(#人员组列表”).hide();
在('s)之后返回此。$el.find('people groups list')\
要添加新组,请单击右上角的+开始操作。

\ '); }否则{ 此.el.find(“#人员组列表”).show(); 集合。每个(功能(项目){ //控制台日志(项目); var显示视图; displayView=新建app.GroupListDisplayView({ 型号:项目, 收款:$collection }); 返回$this.append(displayView.render()); }); 归还这个; } }; GroupListDisplayView.prototype.render=函数(){ var$body; //log(this.model.toJSON()); this.$el.html(this.template({ m:this.model.toJSON() })); $body=this.$el.find(“.card body”); $.each(this.model.get(“人”),function(i,person){ 变异人; this.person=新的app.person({ id:person.id, 化身:person.avatar, 名字:person.first\u name, 姓氏:person.last_name }); console.log(这个人); personTile=新建app.PersonTileView({ 模特:这个人 }); 返回$body.html(personTile.render()); }); 返回此.$el.attr(“id”,“组卡-”+此.model.id); }; GroupListDisplayView.prototype.drop=函数(e){ var$collection,$model,person\u id,request; e、 预防默认值(); $collection=this.collection; person_id=e.originalEvent.dataTransfer.getData('Text'); 请求=新建app.PersonGroupAdd; $model=this.model; 返回请求。保存({ async:true, 等等:是的, person\u id:person\u id, 组id:this.model.get(“id”) }, { 成功:功能(d){ 返回$model.fetch({ async:true, 等等:是吗 }); } }); }; GroupListView.prototype.initialize=函数(){ this.collection.on(“change”,this.renderList,this); this.collection.on(“重置”,this.render,this); 返回这个.renderList(); };
试试这个,把这个函数放在你的收藏中

parse: function (data) {

            data.forEach(function (item) {

                displayView = new app.GroupListDisplayView({
                  model: item,
                  collection: $collection
                });
            });
        }

希望这有帮助。

我不确定
drop
函数与它有什么关系,但我看到
renderList
键控期间被传递
searchName
的结果。可能发生的情况是,
searchName
返回的是一个常规数组,而不是一个包含
each
方法的包装对象(即主干.Collection、jQuery Collection或下划线包装对象)

不要调用
集合。每个
,请使用jQuery或下划线:

$.each(collection, function(i, item) { ... });


这样做,我得到以下未捕获的TypeError:无法调用引用this、this.collection.on(“change”、this.renderList、this)的未定义的方法“on”;this.collection.on(“重置”,this.render,this);在您发布的代码中,我没有看到关于
调用的
,但是您可能需要使用
设置每个
回调的上下文。bind
$。proxy
?是否在
GroupListDisplayView中实例化
GroupListView
?如果没有,那么在哪里?听起来对
新GroupListView
的调用没有传递正确的
集合
参数。我认为问题源于GroupListView.PrototYe没有传递正确的集合,如果我是console.log(collection)在renderList函数中,我得到未定义的返回…如果运行该函数的调用正在通过此函数,为什么会出现这种情况。作为集合参数进行筛选?发布
searchName
函数的源代码。它不是主干网的标准部分。收藏
,所以我不可能知道里面发生了什么。
_.each(collection, function(item, i, list) { ... });