jQueryMobileã使ã£ã¦ ç°¡åã«Ruby on Railsãµã¤ããã¯ã¼ã«ãªiPhone対å¿ãµã¤ãã«ãã¦ã¿ã
jQuery Mobile | jQuery Mobile ã使ã£ã¦ Ruby on Rails ã§ä½ã£ãWebãµã¤ãã iPhoneãªã©ã®ã¹ããã©ã«å¯¾å¿ãããµã¤ãã«å¤æ´ãã¦ã¿ã¾ããã以ä¸ã®ç»å㯠Ruby on Railsã® Scaffold ã§ä½ã£ããµã¤ããjQueryMobile対å¿ããå¾ã®ç»é¢ã§ãã
ã¯ã¼ã«ãªã¹ããã©ãµã¤ãã«ãªã£ã¦ãã¾ãï¼
1. ã¾ãã¯æ®éã«scaffold ã§ã¢ããªãä½ã
Railsã¯javascriptå¨ããå¤§å¹ ã«å¼·åããã3.1ã使ã£ã¦ãã¾ãã
% rails new jq_mobile % cd jq_mobile % rails g scaffold todo due:date task:string % rake db:migration % rails s
2. PC 㨠iPhone ã§view(layout)ãåãæ¿ãã
jQueryMobileç¨ã®HTMLã¯é常ã®PCãµã¤ãç¨ã¨ã¯ç°ãªãã¾ãã®ã§ããã³ãã¬ã¼ããã¡ã¤ã«ãlayoutããPCã¨iPhoneããã®ã¢ã¯ã»ã¹ã§åãæ¿ãããããã«ãã¾ãããã
ãã㯠Railsã®æ©è½ã§ç´ãã«åºæ¥ã¾ãã
config/initializers/mime_types.rb ãã¡ã¤ã«ã«ã³ã¡ã³ãã¢ã¦ãããã¦ãã以ä¸ã®è¡ãæå¹ã«ãã㨠:iphone ã¨ãããã©ã¼ãããã使ããããã«ãªããã³ãã¬ã¼ã㯠XXX.html.erb ã®ä»ã« XXX.iphone.erb ã使ããããã«ãªãã¾ãã
# Be sure to restart your server when you modify this file. # Add new mime types for use in respond_to blocks: Mime::Type.register_alias "text/html", :iphone
ãã¨ã¯ãã³ã³ããã¼ã©ã¼ (app/controllers/application_controller.rb)㧠user_agent ã調ã¹ããã©ã¼ãã¨ãlayout ãåãæ¿ãããã¨ã«ãã¾ãã
class ApplicationController < ActionController::Base protect_from_forgery layout :set_layout before_filter :set_iphone_format def set_iphone_format request.format = :iphone if iphone_request? end def set_layout iphone_request? ? "iphone" : "application" end private def iphone_request? request.user_agent =~ /(Mobile.+Safari)/ end end
3. jQueryMobileã®ã¤ã³ã¹ãã¼ã«
- jQuery Mobileã®javascript, style sheet㯠CDN ã®ãã®ã使ã£ã¦ãè¯ãã®ã§ãããä»å㯠ãã¦ã³ãã¼ãããRailsããã¸ã§ã¯ãã«ã³ãã¼ãã¾ãã
% unizp jquery.mobile-1.0rc1.zip % cd jquery.mobile-1.0rc1 % cp jquery.mobile-1.0rc1.js XXXX/jq_mobile/app/assets/javascripts % cp jquery.mobile-1.0rc1.css XXXX/jq_mobile/app/assets/stylesheets % cp -r images XXXX/jq_mobile/app/assets/stylesheets/images
- app/assets/javascripts/application.jsã¯PCç¨ããã®ã§jQueryMobileãèªã¿è¾¼ã¾ããªãããã«ä»¥ä¸ã®è¡ãåé¤ãã¾ãï¼ãã£ã¨è¯ãããæ¹ãããæ°ãããã»ã»ã»ï¼ã
//= require_tree .
- app/assets/stylesheets/application.css ã¯PCç¨ããã®ã§jQueryMobileãèªã¿è¾¼ã¾ããªãããã«ä»¥ä¸ã®è¡ãåé¤ãã¾ãï¼ãã£ã¨è¯ãããæ¹ãããæ°ãããã»ã»ã»ï¼ã
*= require_tree .
- app/assets/javascripts/mobile_application.js ã追å
//= require jquery //= require jquery_ujs //= require jquery.mobile-1.0rc1
- app/assets/stylesheets/mobile_application.css ã追å
/* *= require_self *= require jquery.mobile-1.0rc1 */
4. iPhoneç¨view, layout ãç¨æ
iPhoneç¨ã«viewportãå®ç¾©ããjQueryMobileã®ãã¼ã¸ç¨divãç½®ãã¾ã
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>iPhone</title> <%= stylesheet_link_tag "mobile_application" %> <%= javascript_include_tag "mobile_application" %> <%= csrf_meta_tags %> </head> <body> <div data-role="page"> <%= yield %> </div> </body> </html>
- app/views/todos/index.iphone.erb ã®è¿½å
ä¾ã¨ãã¦ãTodoä¸è¦§ã表示ãããã¼ã¸ãæ¸ãã¦ã¿ã¾ãããã
ãããã¼ã«addãã¿ã³ãç½®ããããä¸è¦§ãjQueryMobileã®ãã¼ãã«ã¨è¡¨ç¤ºãã¦ãã¾ãã
<div data-role="header"> <h1>Listing todos</h1> <%= link_to 'Add', new_todo_path, "data-icon" => "plus", "class" => "ui-btn-right" %> </div> <div data-role="content"> <ul data-role="listview"> <% @todos.each do |todo| %> <li data-role="list-divider" style="padding: 0.1em 15px;"><%= todo.due %></li> <li><%= link_to todo.task ,todo %></li> <% end %> </ul> </div>
ä»ã®ãã¼ã¸ã¯GitHubã® yuumi3/jQueryMobileRails ã«ç½®ãã¾ããã®ã§åèã«ãã¦ä¸ããã
5. ajaxEnabled ã false ã«ãã
4.ã¾ã§ã§ãjQueryMobile対å¿ã®ãµã¤ããåºæ¥ã¾ãã
iPhoneã·ãã¥ã¬ã¼ã¿ã ãã©ãã¶ã¼ã®user_agentãåãæ¿ãããããã©ã°ã¤ã³ãéçºæ©è½ã使ã£ã¦ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãããã
åããã¦ã¿ãã¨å°ãåä½ãã¸ãã§ãã
Todoä¸è¦§ããTodoãé¸æããç·¨éãä¸è¦§ã«æ»ãã¨ããã®è¡ã¯ç·¨éåºæ¥ãªããªã©ä¸æè°ãªåä½ããã¾ãã»ã»ã»
å®ã¯ãjQueryMobileã¯åç´ã« HTMLã表示ããã ãã§ã¯ãªãããã¼ã¸ããã£ãã·ã¥ãããè²ã
ãªç®¡çãè¡ã£ã¦ãã¾ãããã®çºã«Railsã®ã³ã³ããã¼ã«ããå¤ãã¦ãã¾ããã¨ãå¤ã
ããã¾ãã
ããã§è¨å®ã§ ajaxEnabled ã false ããå¿
è¦ãããã¾ãããã ãããã®è¨å®ã¯ jQueryMobileã®jsãèªã¿è¾¼ãåã«è¡ãå¿
è¦ãããã¾ãã
- app/assets/javascripts/jqm_setup.js ãä½ã
$(document).bind("mobileinit", function(){ $.extend( $.mobile, { ajaxEnabled: false }); });
- app/assets/javascripts/mobile_application.js ã«è¿½å ãã¾ã
//= require jquery //= require jquery_ujs //= require jqm_setup //= require jquery.mobile-1.0rc1
ããã§ãã¡ããã¨ä½¿ããããã«ãªãã¾ãã
6. æ¥ä»è¨å®ç¨ jquery.mobile.datebox
Railsã®date_selectãä½ãå¹´ææ¥ã®3ã¤ã®selectboxã¯jQueryMobileã§ãæ£ããåä½ãã¾ãããã©ããããµã¤ã§ã ^^;
ãã㧠jQuery-Mobile-DateBoxã使ã£ã¦ã¿ã¾ããã
_form.iphone.erbã¯ä»¥ä¸ã®æ§ã«ã·ã³ãã«ã§ããä¸ã®ç»åã®æ§ãªæ¥ä»å
¥åã表示ããã¾ãã
<div data-role="fieldcontain"> <%= f.label :due %> <%= f.text_field :due, "data-role"=>"datebox", "data-options"=>'{"mode": "flipbox"}' %> </div>
ãã ããããã©ã«ãã¯ç±³å½å¼è¡¨ç¤ºãªã®ã§ã以ä¸ã®ãããªè¨å®ã§æ¥æ¬å¼ã«ã§ãã¾ãã
jQuery.extend(jQuery.mobile.datebox.prototype.options, { titleDateDialogLabel: 'æ¥ä»è¨å®', setDateButtonLabel: 'OK', dateFormat: 'YYYY-mm-dd', headerFormat: 'YYYY-mm-dd', dateFieldOrder: ['y', 'm', 'd'], monthsOfYear: ["1æ","2æ","3æ","4æ","5æ","6æ","7æ","8æ","9æ","10æ","11æ","12æ"], monthsOfYearShort: ["1æ","2æ","3æ","4æ","5æ","6æ","7æ","8æ","9æ","10æ","11æ","12æ"] });
GitHubã« ããã¸ã§ã¯ãå
¨ä½ã yuumi3/jQueryMobileRails ã«ç½®ãã¾ããã®ã§åèã«ãã¦ä¸ããã