Backbone.jsの準備
まずは必要なライブラリのダウンロード。
$wget http://code.jquery.com/jquery-1.10.1.min.js
$git clone https://github.com/documentcloud/underscore.git
$cp underscore/underscore-min.js .
$git clone https://github.com/douglascrockford/JSON-js.git
$cp JSON-js/json2.js .
$wget http://backbonejs.org/backbone-min.js
次は、テスト用のhtmlとjsを作成。
<!DOCTYPE html>
<html>
<head>
<title>Backbone Example</title>
</head>
<body>
<h1>Backbone Example</h1>
<script src="jquery-1.10.1.min.js"></script>
<script src="json2.js"></script>
<script src="underscore-min.js"></script>
<script src="backbone-min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
(function() {
console.log("Hello Backbone!");
}());
動作確認して準備OK!
coffee scriptを加えてみる
ここを
参考にしてnpmを入れる。
package.jsonを書いて必要なライブラリをインストールする.
{
"name": "sample",
"version": "0.0.1",
"dependencies": {
"coffee-script": "*",
"jasmine-node": "*",
"grunt": "*"
}
}
$npm install
ビルドを簡易化するためにCakefileも用意しておく。
fs = require 'fs'
path = require 'path'
{spawn, exec} = require 'child_process'
coffee = (args) ->
proc = spawn './node_modules/.bin/coffee', args
proc.stderr.on 'data', (buffer) -> console.log buffer.toString()
proc.on 'exit', (status) -> process.exit(1) if status != 0
jasmine = (args) ->
proc = spawn './node_modules/.bin/jasmine-node', args
proc.stdout.on 'data', (buffer) -> console.log buffer.toString()
proc.on 'exit', (status) -> process.exit(1) if status != 0
task 'build', "build source files in './src' to './build'", ->
target = [
'src/main'
]
coffee ['-j','tmp','-o','lib','-c'].concat target
exec 'sleep 1 && cat build/tmp.js | grep -v "exports\." | grep -v "require(" > build/app.js', (err) ->
throw err if err
task 'buildspec', "build source files in './spec' to './lib'", ->
target = [
'src/main'
'spec/main_spec'
]
coffee ['-j','tmpspec','-o','lib','-c'].concat target
exec 'sleep 1 && cat build/tmpspec.js | grep -v "exports\." | grep -v "require(" > build/main_spec.js', (err) ->
throw err if err
task 'clean', 'clean out temporary build files', ->
if fs.readdirSync('lib/').length > 0
exec 'rm js/*.js', (err) ->
throw err if err
task 'spectest', 'run jasmine-node spec suite', ->
invoke 'build'
jasmine ['--coffee','--verbose','spec/']
task 'all', 'clean and build all', ->
invoke 'clean'
invoke 'build'
invoke 'buildspec'
これで準備OK
$vim src/app.coffee
obj = new Backbone.Model()
obj.set({name: "Murata"})
obj.set({age: 20})
console.log "obj: " + JSON.stringify(obj)
console.log "obj.name: " + obj.get("name")
$cake build
ブラウザアクセスしてさっきと同じならOK
参考ページ:
http://appkitbox.com/knowledge/javascript/2013/03/15/backbone-1