LoginSignup
2
2

More than 3 years have passed since last update.

backbone.js - 環境準備

Last updated at Posted at 2020-02-15

概要

普段は、vue.jsなどを使っていますが、backbone.jsを使うことがあったので、学習してみる。

ローカル環境整備

dockerでnginxを起動して環境を整える。

ターミナル
$ docker-compose build
$ docker-compose up -d

※ 学習ように作ったコードはgithubにアップしています
https://github.com/reflet/sample-backbone.js

サンプルコード

とりあえず、必要なjsを読み込んで、動かしてみました。

./src/html/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>Backbone.js学習</title>
</head>
<body>
    <h1>TOP Page</h1>
    <p>ひとまず動く環境を整えてみる。</p>
    <script src="https://cdn.jsdelivr.net/npm/underscore@1.9.1/underscore.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/jquery/jquery@3.4.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/backbone@1.4.0/backbone.min.js"></script>
    <script>
        const Person = Backbone.Model.extend({
            defaults: {
                country: '日本',
                prefecture: '東京',
            },
            hello: function() {
                console.log('こんにちは、' + this.get('name') + 'さん');
            }
        });
        const user = new Person({name: '太郎', age: 30});

        // データ確認
        console.log(user.toJSON()); // {country: "日本", prefecture: "東京", name: "太郎", age: 30}

        // 名前を取得する
        console.log(user.get('name'));

        // 独自定義した関数を実行してみる
        user.hello();
    </script>
</body>
</html>

動作確認

正常に処理され、コンソールに結果が表示されたようです。
sample.png

まとめ

underscore.min.js, jquery.min.js, backbone.min.js を読み込めば使える点は、vue.jsと同じでお手軽で導入コストは低そうですね。

参考サイト

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2