すべて、私は Backbone の初心者です。バックボーンのモデルを理解しようとしています。特にモデルの定義方法。これまでのところ、バックボーンのモデルを定義する方法について、明確または正式な方法は見当たりませんでした。たとえば、ヘルプ ドキュメントの set メソッドを見てみましょう。
設定
model.set(属性、[オプション])
モデルに属性のハッシュ (1 つまたは複数) を設定します。
以下のようなコードがあるとします。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
。私が理解するのを助けるために、それについてもっと教えてくれる人はいますか? ありがとう。