VladimirZhid
@VladimirZhid
Нравится делать что-то интересное и полезное.

Почему не идет обработка .each?

//Create spacename for this App
var CommentApp = {};
var commentsCollection;
var commentsView;
//collection
var comments = [
  {
    userid: 1,
    commentid: 1,
    commentfor: 2,
    username: "Айжана",
    userlastname: "Жолдошбекова",
    userphoneurl: "/uploads/user/2.jpg",
    post_date: "15.12.15",
    commenttext: "Коммент №1"
  },
  {
    userid: 2,
    commentid: 2,
    commentfor: 1,
    username: "Сапаргуль",
    userlastname: "Мамытова",
    userphoneurl: "/uploads/user/3.jpg",
    post_date: "15.11.16",
    commenttext: "Коммент №2"
  },
  {
    userid: 2,
    commentid: 2,
    commentfor: 1,
    username: "Эрнест",
    userlastname: "Джапаров",
    userphoneurl: "/uploads/user/6.jpg",
    post_date: "15.11.15",
    commenttext: "Коммент №3"
  },
  {}
];


$(function () {
//Prototype comment item
  CommentApp.CommentModel = Backbone.Model.extend({
    defaults: {
      userid: 0,
      commentid: 0,
      commentfor: 0,
      username: "name",
      userlastname: "lastname",
      userphoneurl: "/uploads/user/4.jpg",
      post_date: "15.11.15",
      commenttext: "SomeText"
    }
  });

//Prototype comments collection
  CommentApp.CommentColletion = Backbone.Collection.extend({
    model: CommentApp.CommentModel,
    initialize: function(){
      console.log(this);
      commentsView = new CommentApp.CommentsView({collection: this})
    }
  });

//View of comment item (CommentApp.CommentModel)
  CommentApp.CommentView = Backbone.View.extend({
    tagName: "div",
    className: "comments_item clear_c",
    template: $("#comment_item_template").html(),
    initialize: function(){},
    render: function () {
      //
      var tmpl = _.template(this.template);
      this.$el.html(tmpl(this.model.toJSON()));
      return this;
    }
  });


  CommentApp.CommentsView = Backbone.View.extend({
    el: '.comments_list',
    initialize : function(){
      this.render();
    },
    render: function(){
      var _this = this;
      this.collection.each(function(comment){
        console.log(comment);
        var viewComment = new CommentApp.CommentView({model: comment});
        _this.$el.append(viewComment.render().$el);
      });
      return this;
    }
  });

  commentsCollection = new CommentApp.CommentColletion(comments);

});


Как видите в модели коллекции(CommentApp.CommentColletion.initialize()) я инициализирую вьюшку, но по какой-то причине обход в CommentApp.CommentsView.render() по коллекции не происходит, ошибку тоже не выдает.

Помогите плиз. Пока решил проблему тем, что вьюшку инициализирую отдельно и все норм, но причину не могу понять
  • Вопрос задан
  • 87 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы