0

ビューにデータを入力するのに役立つコレクションをループするのに問題があります。コレクションをループしようとすると、次のエラーが発生します。

キャッチされていない TypeError: オブジェクト # にはメソッド 'each' がありません

オブジェクトに明らかにそのメソッドがないことを除いて、なぜこのエラーが発生するのかまったくわかりませんが、ドロップ関数を実行したときにのみこのエラーが発生します。以下のコードを参照してください。

これがバックボーンコードによるものです。

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();
    };
4

2 に答える 2

0

代わりにこれを試して、この関数をコレクションに配置してください

parse: function (data) {

            data.forEach(function (item) {

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

お役に立てれば。

于 2013-06-25T16:20:51.253 に答える
0

関数がそれと何の関係があるのか​​ はわかりませんが、 duringの結果が渡されていることがdropわかります。メソッドを持つラップされたオブジェクト(つまり、Backbone.Collection、jQuery コレクション、または Underscore でラップされたオブジェクト)ではなく、通常の配列が返されている可能性があります。renderListsearchNamekeyupsearchNameeach

を呼び出す代わりにcollection.each、jQuery または Underscore を使用します。

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

また

_.each(collection, function(item, i, list) { ... });
于 2013-06-25T16:40:08.813 に答える