Backbone.js を始めてみた。そのHTMLテンプレートの覚え書き。
[html]
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello Backbone.js</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="http://backbonejs.org/backbone-min.js"></script>
<script type="text/javascript">
(function() {
//Model作成
var Animal = Backbone.Model.extend({
//Attribute(属性)
defaults: {
species: "Animal",
name: "None"
},
//コンストラクタ
// constructor: function(){
// console.log("初期化処理");
// },
//メソッド
call: function(){
console.log("動物 : " + this.get("species"));
console.log("私の名前は" + this.get("name") + "です。");
}
});
//Animalのインスタンス化
var none = new Animal();
//Animal(犬)のインスタンス化
var dog = new Animal({
species: "犬",
name: "ポチ"
});
//Animal(猫)のインスタンス化
var cat = new Animal({
species: "猫",
name: "タマ"
});
//console.log(dog);
//console.log(cat);
//console.log(dog.toJSON());
none.call();
dog.call();
cat.call();
})();
</script>
</head>
<body>
<h3>Backbone.js</h3>
</body>
</html>
[/html]
- コンストラクタを定義した場合、callメソッド内で、属性を取得しようとthis.get(“name”)するとエラーとなる。
- dog.toJSON()をコールするとエラーとなる。メソッドfunctionまでは、JSON化できないからかな?
別の機会に検証しようと思う。