# Choco **Repository Path**: mirrors/Choco ## Basic Information - **Project Name**: Choco - **Description**: Choco 将MVC带到了客户端!一个Choco应用仅有一个HTML页面组成,所有的交互有JS来完成 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: https://www.oschina.net/p/choco - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-01-04 - **Last Updated**: 2025-08-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README = Choco A delicious Javascript web framework made in Belgium! Choco brings the MVC to the client side! You like Javascript and you want to develop rich internet applications? You also know that HTML & CSS are powerful? Cappuccino & Sproutcore don't feel like web development anymore? Thanks to Choco, you'll be able to easily develop maintainable web applications. A Choco app consists of only one HTML page, all the interactions are managed by Javascript. Your UI only uses HTML and CSS! Choco is based on powerful libaries : * Sammy (http://github.com/quirkey/sammy) * js-model (http://github.com/benpickles/js-model) * Jim (http://github.com/quirkey/jim) Huge thanks to Aaron Quint and Ben Pickles for their incredible work. A sample project based on Rails 3 is available here : http://github.com/ahe/choco_demo An awesome screencast with an awesome belgian accent is available here : http://www.2dconcept.com/images/choco.mov Follow us on Twitter : https://twitter.com/choco_js == Installation Choco is a Ruby gem, simply install it : $ gem install choco == A new application $ choco new my_project This will generate your project structure. You can then install the required JS dependencies (jQuery, Sammy, ...) by executing the following command at the root of your project : $ rake choco:js:install Launch your local server : $ choco server == Location A Choco app is composed of static files (js, css, images, ...), it must be directly accessible on your web server. A local Rack web server can be launched directly using the '$ choco server' command. You'll have to install WEBRick or Mongrel. If you use Rails, you can for example put your Choco app inside the public/javascripts folder. Once you've chosen the location where your application will reside, don't forget to configure the path to your view files. You can find the line to edit in your application_controller (app/controllers). Example for Rails : this.project_path = '/javascripts/my_project'; == Jim Jim is mix of Ruby gems & Bundler but for Javascript libraries. When you install a JS library using the $ jim install command, it is stored in your home folder (~/.jim/lib). All your projects can then use this repository to load the required dependencies. The $ rake choco:js:install command installs all the dependencies you need to run a Choco app. A Jimfile is located at the root of your project, it lists all these dependencies. They can be libraries (jQuery, Sammy, ...) but also local JS files of your application (controllers, models, ...). All these files are bundled into the compressed/bundled.js file, so you only have to include this file in your HTML page and all your Choco app will be loaded. You can continuously track changes in your JS files by running the following command in your project root folder : $ choco --watch The watch script will track your JS files (creation, edition, suppression) and automatically update your Jimfile & bundled.js to reflect these changes. You never have to worry about including your JS files inside your HTML page, just include bundled.js! == Access your homepage To launch your application, just call the index.html page. I'm using Rails, I will call the following URL in my web browser : http://localhost:3000/javascripts/choco_app/index.html Notice that when the app is launched, the URL changes to : http://localhost:3000/javascripts/choco_app/index.html#/main #/main is a route in your Choco app (thanks to Sammy), it is defined in your ApplicationController. this.get('#/main', function(cx) { }); You have to include this # in every HTML link you add into your views. List all the posts Add a new post ... = Project structure A Choco project has the following structure : === Jimfile This file list all the dependencies for your project (libraries & local files). === app It contains four sub-folders : controllers, helpers, models & views. This is where you will spend most of your development time. === compressed This is where the bundled.js file is created, it bundles all your JS files in one single file. It is highly recommended to compress this file before going live (rake choco:deploy). === images Store all the images used by your application in this folder. === index.html This is the file you have to open in your browser to launch your Choco application. It includes your bundled.js file, your stylesheets and defines the layout of your application. The #choco div is very important, this is where the HTML generated by your views will be inserted. If you change his name, you must configure it into the app/controllers/application_controller.js as well. === lib Use this folder to store your JS files which are specific to your project but don't have their place in the app folder. If this is a library that can be used by other projects, you should use Jim instead. === script This folder contains the choco script file. It brings you a few generators (model, controller, layout, scaffold, json, plugin). === spec Test your application using Behavior Driven Development (BDD). === stylesheets Store all the stylesheets used by your application in this folder. = A first resource (CRUD) First of all, don't forget to start the choco watcher ($ choco --watch) if you don't want to update your Jimfile manually. The easiest way to get started is to generate a scaffold based on JSON. Use your server side technology to create a web service that returns JSON representing your resource. For example, with Rails : def index render :json => Post.all end Once you have that, you can generate your scaffold very easily : $ choco generate scaffold post http://localhost:3000/posts This will create the PostsController with all the actions, the views and the model. Notice that the Choco watcher automatically updated your Jimfile and your bundled.js. It also updated your ApplicationController by adding the following line : PostsController(this); You can now call this new page with the following URL : http://localhost:3000/javascripts/choco_app/index.html#/posts This will send a GET request to the #/posts URL of your application. This request will be handled by your PostsController and more precisely by this action : get('#/posts', function(cx) { cx.posts = Post.all(); }); We are using our model to get all the posts (locally) and store them in the posts variable. Using cx will make this variable available in your view. Like Rails, Choco will automatically render the view with the same name as the current action (located in the view folder with the same name as the current controller). In our case app/views/posts/index.template will be rendered. This is a simple HTML page with some Javascript tags. Here is a part of it : <% $.each(posts, function(i, post) { %>