Monday, February 7, 2011

Javascript MVC

This a practice to use Javascript MVC.

1. Download javascriptmvc from http://www.javascriptmvc.com/.

2. Move/Unzip and cd to your jmvc folder

3. Updates every sub libraries.
  ./js documentjs/update
./js funcunit/update
./js jquery/update
./js steal/update


4. Generates your app
 ./js jquery/generate/app cookbook
cookbook/cookbook.css
cookbook/cookbook.html
cookbook/cookbook.js
cookbook/controllers
cookbook/docs
cookbook/fixtures
cookbook/funcunit.html
cookbook/models
cookbook/qunit.html
cookbook/resources
cookbook/scripts
cookbook/scripts/build.html
cookbook/scripts/build.js
cookbook/scripts/clean.js
cookbook/scripts/docs.js
cookbook/test
cookbook/test/funcunit
cookbook/test/funcunit/cookbook_test.js
cookbook/test/funcunit/funcunit.js
cookbook/test/qunit
cookbook/test/qunit/cookbook_test.js
cookbook/test/qunit/qunit.js
cookbook/views


5. Make a Create-Read-Update-Delete (CRUD) scaffold.
 $ ./js jquery/generate/scaffold Cookbook.Models.Recipe 
cookbook/controllers
cookbook/controllers/recipe_controller.js
cookbook/fixtures
cookbook/fixtures/recipes.json.get
cookbook/models
cookbook/models/recipe.js
cookbook/test
cookbook/test/funcunit
cookbook/test/funcunit/recipe_controller_test.js
cookbook/test/qunit
cookbook/test/qunit/recipe_test.js
cookbook/views
cookbook/views/recipe
cookbook/views/recipe/edit.ejs
cookbook/views/recipe/init.ejs
cookbook/views/recipe/list.ejs
cookbook/views/recipe/show.ejs

Here's what each part does:

recipe_controller.js
Cookbook.Controllers.Recipe, like all Controllers, respond to events such as click and manipulate the DOM.
edit.ejs,init.ejs,list.ejs,show.ejs
Views are JavaScript templates for easily creating HTML.
recipe_controller_test.js
Tests the CRUD functionality of the user interface.
recipe.js
Cookbook.Models.Recipe model performs AJAX requests by manipulating services.
recipes.get
Fixtures simulate AJAX responses. This fixture responds to GET '/recipes'.
recipe_test.js
A unit test that tests Recipe model.


6. Including Scripts
After generating the scaffolding files, you must steal them in your application file. Open cookbook/cookbook.js and modify the code to steal your recipe controller and model as follows:
 steal.plugins(    
'jquery/controller',
'jquery/controller/subscribe',
'jquery/view/ejs',
'jquery/controller/view',
'jquery/model',
'jquery/dom/fixture',
'jquery/dom/form_params')
.css('cookbook')
.resources()
.models('recipe')
.controllers('recipe')
.views();

cookbook/test/qunit/qunits.js
 steal  
.plugins("funcunit/qunit", "cookbook")
.then("cookbook_test","recipe_test")

cookbook/test/funcunit/funcunit.js
 steal  
.plugins("funcunit")
.then("cookbook_test","recipe_controller_test")


7. That's it. open cookbook/cookbook.html

No comments: