所有,我是Backbone的新手。我正在尝试了解 Backone 的模型。特别是如何定义模型。到目前为止,我还没有看到关于如何为骨干定义模型的清晰或正式的方法。例如让我们看看帮助文档中的 set 方法。
放
model.set(属性,[选项])
在模型上设置属性的散列(一个或多个)。
假设我们有一些如下代码。我认为set方法实际上是为模型分配一个 javascript 对象。
    window.Employee = Backbone.Model.extend({
      validate:function(attrs){
        for(var key in attrs){
            if(attrs[key] == ''){
                return key + "can not be null";
            }
            if(key == 'age' && isNaN(attrs.age)){
                return "age is numeric";
            }
        }
      }
    });
    ....
    var attr = {}; // I can't not sure what is {} mean.
    $('#emp-form input,#emp-form select').each(function(){
        var input = $(this);//using jquery select input and select. and enumerate all of them.
        attr[input.attr('name')] = input.val();//I am not sure what does it means
    });
    if(employee.set(attr)){
            Employees.create(employee);
        }
    ....
在这个例子中,我没有看到我们可以在 java 类或 c# 类中看到的经典方式来定义类字段或方法。但只看到一个validate功能。有没有人可以告诉我更多关于它的信息以帮助我理解?谢谢。