1

由于某些原因,我不断收到此错误,(请参阅随附的屏幕截图)。我尝试添加一个_.bindAll(this);,甚至尝试升级我的代码以获得最新版本的主干js。仍然没有运气。

有人可以帮我吗?

在此处输入图像描述

var app = app || {};

(function ($) {
'use strict';

app.EmployeeView = Backbone.View.extend({
    el: '#container',
    model: app.Employee,
    events: {
        'click #save' : 'saveEntry'
    },
    initialize: function(){
        console.log('Inside Initialization!');
        this.$empName = this.$('#txtEmpName');
        this.$department = this.$('#txtDepartment');
        this.$designation = this.$('#txtDesignation');
        this.listenTo(app.employees, 'add', this.addEmployee);
        app.employees.fetch();
        console.log('End of Initialization!');
        //this.render();
    },
    render: function () {
        console.log('Inside Render!!');
        console.log(this.model);
        this.$el.html(this.template(this.model.toJSON()));
        console.log('Inside End of Render!!');
        return this;
    },
    newAttributes: function(){
        return{
            empName: this.$empName.val(),
            department: this.$department.val(),
            designation: this.$designation.val()
        };
    },

    saveEntry: function(){
        console.log('Inside SaveEntry!');
        //console.log(this.newAttributes());
        console.log('this.model');
        console.log(app.Employee);
        //app.employees.create(this.newAttributes());
        app.Employee.set(this.newAttributes());
        app.employees.add(app.Employee);
        console.log('After SaveEntry!');
    },
    addEmployee: function (todo) {
        var view = new app.EmployeeItemView({ model: app.Employee });
        $('#empInfo').append(view.render().el);
    }
})
})(jQuery);

“collections/employees.js”的代码

var app = app || {};
(function (){
    console.log('Inside collection');
    var Employees = Backbone.Collection.extend({
        model: app.Employee,
        localStorage: new Backbone.LocalStorage('employee-db')
    });
    app.employees = new Employees();
})();

“model/employee.js”的代码

var app = app || {};
(function(){
    'use strict';

    app.Employee = Backbone.Model.extend({
        defaults: {
            empName: '',
            department: '',
            designation: '' 
        }
    });
})();
4

2 回答 2

5

你在你看来是这样说的:

model: app.Employee

app.Employee看起来像模型“类”而不是模型实例。您的视图希望其model属性中有一个模型实例。通常你会这样说:

var employee = new app.Employee(...);
var view = new app.EmployeeView({ model: employee });
于 2013-09-15T17:26:03.663 回答
1

this.model.toJSON()将不起作用,因为this.modelapp.Employee构造函数。EmployeeView.render实际上,我认为您的方法没有任何意义。如果它是聚合视图,为什么你有模型呢?否则第二个视图类是EmployeeItemView什么?如果您正在关注 ToDo MVC 示例,您会看到 中没有模型AppView,这就是为什么我得出结论您不需要在EmployeeView. 而且render您提供的方法似乎属于EmployeeItemView.

其次,您调用app.Employee.setwhich 也是对构造函数的调用,而不是对对象的调用。我想你的意思是

saveEntry: function(){
    console.log('Inside SaveEntry!');
    app.employees.create(this.newAttributes());
    console.log('After SaveEntry!');
},

如果您想将模型传递给app.EmployeeItemView您应该使用回调参数。

addEmployee: function (employee) {
    var view = new app.EmployeeItemView({ model: employee });
    $('#empInfo').append(view.render().el);
}
于 2013-09-15T17:57:28.170 回答