2013-06-25 3 views
0

У меня возникли проблемы с переходом через коллекцию, которая помогает заполнить представление данными. Когда я пытаюсь Переберите коллекции и получить следующую ошибку,backbone - не может пройти через коллекцию

Uncaught TypeError: Object # has no method 'each'

У меня Absolutley не знаю, почему я получаю эту ошибку, кроме объекта obviosly не имеет такого метода, но я только получаю это ошибка, когда я запускаю функцию 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(); 
    }; 

ответ

0

Попробуйте вместо этого, поместите эту функцию в вашей коллекции

parse: function (data) { 

      data.forEach(function (item) { 

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

Надеется, что это помогает.

0

Я не уверен, что функция drop должен сделать с ним, но я вижу, renderList передается результаты searchName во keyup. Вероятно, что searchName возвращает обычный массив, а не обернутый объект, который будет иметь метод each (т. Е. Коллекция Backbone.Collection, коллекция jQuery или обернутый Underscore).

Вместо вызова collection.each, использовать JQuery или Подчеркивание:

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

или

_.each(collection, function(item, i, list) { ... }); 
+0

Выполнение этого я получаю следующее, неперехваченным TypeError: Невозможно вызвать метод 'на' неопределенной Который ссылается на это, this.collection.on ("change", this.renderList, this); this.collection.on ("reset", this.render, this); – Udders

+0

Я не вижу эти вызовы 'on' в коде, который вы отправили, но, возможно, вам нужно установить контекст обратного вызова' each' с '_.bind' или' $ .proxy'? – freejosh

+0

добавить колл в код в исходном сообщении – Udders