0

我目前正在学习 Backbone.js 之类的,我有点难以理解如何从集合中获取模型。事实是,我不知道我是否以正确的方式将新模型绑定到集合。这是我的代码:

  var app = {};

  app.Answer = Backbone.Model.extend({
    defaults: {
      title: 'Your answer here',
      isAnswer: false,
      number: 0
    }
  });

  // var answer = new app.Answer();

  app.AnswerList = Backbone.Collection.extend({
    model: app.Answer,
    currentStatus : function(status){
      return _(this.filter(function(data) {
          return data.get("isAnswer") == status;
      }));
    },
    // localStorage: new Store("backbone-answers")
  });

  // instance of the Collection
  app.answerList = new app.AnswerList();

  app.AnswerView = Backbone.View.extend({
    tagName: 'li',
    template: _.template($('#answer-template').html()),

    events: {
      'click button.destroy': 'destroy',
      'change input.answer-edit' : 'updateLabel',
      'click button.edit' : 'editLabel',
      'click button.save' : 'saveLabel',
      'change input.toggle': 'toggleAnswer',      
    },

    initialize: function(){
      _.bindAll(this, 'render','remove', 'unrender', 'updateLabel', 'editLabel', 'saveLabel','toggleAnswer');
      this.model.bind('change', this.render);
      this.model.bind('destroy', this.unrender);
      this.render();
    },
    render: function(){
      $(this.el).html(this.template(
        {
          title: this.model.get('title'), 
          isAnswer: this.model.get('isAnswer'), 
          number: this.model.get('number')
        }
      ));

      $(this.el).find('.answer-edit').hide();
      $(this.el).find('.save').hide();

      return this;
    },
    unrender: function(){
      // console.log('unrender');
      $(this.el).remove();
    },
    destroy: function(){
      // console.log('destroy');
      this.model.destroy();
    },
    editLabel: function(e){
      $(e.target).hide();
      $(this.el).find('.save').show();
      $(this.el).find('.answer-text').hide();
      $(this.el).find('.answer-edit').show();
    },
    updateLabel: function(e){
      var newText = $(e.target).val();
      this.model.set({title: newText});
    },
    saveLabel: function(e){
      $(e.target).hide();
      $(this.el).find('.edit').show();
      $(this.el).find('.answer-edit').hide();
      $(this.el).find('.answer-text').show();
    },
    toggleAnswer: function(e){
      e.stopPropagation();
      if($(e.target).val() == 'on'){
        this.model.set({ isAnswer: true });
      } else {
        this.model.set({ isAnswer: false });
      }
    }
  });


  app.AppView = Backbone.View.extend({
    el: $('#body'),

    events: {
      'click button#insert-button': 'addAnswer',
      'click button#get-answer': 'getAnswer',
    },

    initialize: function(){
      _.bindAll(this, 'addAnswer');

      this.collection = new app.AnswerList();
      this.counter = 0;
    },
    addAnswer: function(){
      this.counter++;
      var answer = new app.Answer();
      answer.set({
        number: this.counter
      });

      var newAnswer = new app.AnswerView({model: answer});

      $('ul',this.el).append(newAnswer.render().el);
    },
    getAnswer: function(){
      var theAnswer = this.collection.where({isAnswer: true});
      console.log(theAnswer);
      // alert(theAnswer);
    }

  });

  app.appView = new app.AppView();

我有一个模型、一个集合和两个视图,一个视图处理一段代码(answerView部分),另一个视图添加答案(并处理集合部分)。因此,当我尝试返回带有 的模型时this.collection.where({isAnswer: true}),它返回一个空数组。事实上,当我试图返回整个集合对象时,我似乎在它的模型数组中没有任何模型。所以,我怀疑我在向集合中插入新模型时做错了。

我如何确定我的收藏中有模型,并且我以正确的方式返回指定的模型?有任何想法吗?

4

1 回答 1

1

代码看起来是正确的,除了一件事:您实际上并没有将新的 Answer 模型添加到集合中。尝试在方法collection.add内部使用addAnswer

this.collection.add(newAnswer);
于 2013-08-30T22:11:37.933 回答