简体   繁体   English

Backbone.js-无法从集合中获取指定的模型

[英]Backbone.js - cannot get specified model from collection

I'm currently learning Backbone.js and like and I'm having a little dificult to understand how I can get a model from a collection. 我目前正在学习Backbone.js并喜欢它,我有点难以理解如何从集合中获取模型。 The fact is that I don't know if I'm doing the right way of bind new models to a collection. 事实是我不知道我是否在执行将新模型绑定到集合的正确方法。 Here is my code: 这是我的代码:

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

I have a model, a collection and two views, one view that handles a piece of code (the answerView part), and another view that adds the answers (and deals with the collection part). 我有一个模型,一个集合和两个视图,一个视图处理一段代码( answerView部分),另一个视图添加答案(并处理集合部分)。 So, when I try to return a model with this.collection.where({isAnswer: true}) , It returns an empty array. 因此,当我尝试使用this.collection.where({isAnswer: true})返回模型时,它将返回一个空数组。 In fact, when I try just to return the entire collection object, I seems that it isn't any model in it's array of models. 实际上,当我尝试返回整个集合对象时,似乎它不是模型数组中的任何模型。 So, I'm suspecting that I'm doing something wrong about inserting new models to the collection. 因此,我怀疑在向集合中插入新模型时做错了什么。

How can I be sure that there is models in my collection, and that I returning the right way the specified model? 如何确定集合中有模型,并且以正确的方式返回指定模型? Any ideas? 有任何想法吗?

The code looks correct, except for one thing: you're not actually adding the new Answer model to the collection. 该代码看起来正确,除了一件事:您实际上没有在集合中添加新的Answer模型。 Try using collection.add inside the addAnswer method: 尝试在addAnswer方法中使用collection.add

this.collection.add(newAnswer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM