TypeError:操作数n中的'in'无效

时间:2014-05-22 06:43:06

标签: javascript backbone.js underscore.js

在萤火虫中,我有一个错误,它告诉我这是下划线中的类型错误.min.js

当我推荐月份.js中的错误行时,没有错误

我不知道我犯了什么错,我检查了monthCollection,我确定monthCollection是对的

我无法访问DayView的初始化,日志没有“成功”但只有一行'开始新的日视图'

JS /视图/ month.js

define([
  'backbone',
  'underscore',
  'views/day'
], function(Backbone, _, DayView) {
  'use strict';

  var MonthView = Backbone.View.extend({
    initialize: function(el, collection) {
      this.el = el;
      this.monthCollection = collection;

      this.render();
    },

    //draw the month
    render: function() {
      this.monthCollection.each(function(date) {
        var tmpDate = date.get("date");
        var tmpDay = date.get("day");
        console.log("begin to new a day view");
        var dayView = new DayView(tmpDate, tmpDay);  //error
//        this.el.append(dayView.view);
      }, this);
    }
  });

  return MonthView;
});

JS /视图/ day.js

define([
  'backbone',
  'underscore'
], function(Backbone, _) {
  'use strict';

  var DayView = Backbone.View.extend({
    dateTemplate: _.template( $('#date-template').html() ),

    initialize: function(date, day) {
      console.log("success");
      this.date = date;
      switch(day) {
        case 0:
          this.day = Sun;
        break;
        case 1:
          this.day = Mon;
        break;
        case 2:
          this.day = Tue;
        break;
        case 3:
          this.day = Wen;
        break;
        case 4:
          this.day = Thu;
        break;
        case 5:
          this.day = Fri;
        break;
        case 6:
          this.day = Sat;
        break;
        default:
          this.day = fail;
      }

      this.render();
    },

    //draw the day to this.view
    render: function()  {
      this.view = this.dateTemplate({
        date: this.date,
        day: this.day
      });
    }
  });

    return DayView;
});

3 个答案:

答案 0 :(得分:0)

您没有以正确的方式将参数传递给视图。查看http://backbonejs.org/#View-constructor

这是一个完整的解决方案:

var startData = [
    {
        "date": "18/01/1983",        
        "day" : 3
    },
    {
        "date": "20/01/1983",        
        "day" : 5
    }
];

var Day = Backbone.Model.extend();

var Month = Backbone.Collection.extend({
    model: Day
});


var MonthView = Backbone.View.extend({
    initialize: function() {
      this.render();
    },

    //draw the month
    render: function() {
      this.collection.each(function(date) {
        var tmpDate = date.get("date");
        var tmpDay = date.get("day");

        var dayView = new DayView({date:tmpDate, day:tmpDay });  

        this.$el.append(dayView.el);
      }, this);
    }
});

 var DayView = Backbone.View.extend({

    initialize: function(options) {

      this.date = options.date;

      switch(options.day) {
        case 0:
          this.day = "Sun";
        break;
        case 1:
          this.day = "Mon";
        break;
        case 2:
          this.day = "Tue";
        break;
        case 3:
          this.day = "Wen";
        break;
        case 4:
          this.day = "Thu";
        break;
        case 5:
          this.day = "Fri";
        break;
        case 6:
          this.day = "Sat";
        break;
        default:
          this.day = "fail";
      }

      this.render();
    },

    render: function()  {

        var template = _.template($('#date-template').html(), {        
                            date: this.date,
                            day: this.day});
        this.$el.html(template);

    }
});

var month = new Month(startData);   
var monthView = new MonthView({el: $('body'), collection:month});

答案 1 :(得分:0)

试试这个:

var Day = Backbone.Model.extend({
  dayOfWeek: ["Sun","Mon","Tue","Wen","Thu","Fri","Sat"],
  defaults: {
    date: "01/01/1970",
    day : 4
  },
  toJSON: function(){
    var json = Backbone.Model.prototype.toJSON.apply(this, arguments);
    json.day = this.dayOfWeek.hasOwnProperty(json.day) ?
               this.dayOfWeek[json.day] : 'fail';
    return json;
  }
});

var DayView = Backbone.View.extend({
  template: _.template($('#date-template').html()),
  initialize: function() {
    this.model = this.model || new Day();
    return this.render();
  },
  render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  }
});

var Month = Backbone.Collection.extend({ model: Day });
var MonthView = Backbone.View.extend({
  initialize: function() { return this.render() },
  render: function(){
    var fragment = document.createDocumentFragment();
    this.collection.each(function (day){
      var dayView = new DayView({ model: day });
      fragment.appendChild(dayView.el);
    }, this);
    this.$el.html(fragment);
    return this;
  }
});

var monthView = new MonthView({
  el: document.body,
  collection: new Month(startData)
});

答案 2 :(得分:0)

此错误曾经发生在我身上,因为我将参数作为字符串传递给视图而不是对象。