Upgrade to Pro — share decks privately, control downloads, hide ads and more …

社内勉強会資料 PHPプロジェクトでのJavaScript

 社内勉強会資料 PHPプロジェクトでのJavaScript

社内勉強会でJSについて話した時の資料

jQueryについてのちょっろとした話と
JSの中にPHPのタグを入れざるを得ない時の対処法を考えてみた

shmurakami

April 28, 2014
Tweet

More Decks by shmurakami

Other Decks in Programming

Transcript

  1. ready(handler) Description: Specify a function to execute when the DOM

    is fully loaded. DOMͷಡΈࠐΈ͕׬ྃͨ͠λΠϛϯάͰ handler͕࣮ߦ͞ΕΔ
  2. ready(handler) Description: Specify a function to execute when the DOM

    is fully loaded. DOMͷಡΈࠐΈ͕׬ྃͨ͠λΠϛϯάͰ handler͕࣮ߦ͞ΕΔ
  3. (function(App){ var Edit = (function(){ return { isText: function(type){return type

    === Hoge.TypeText; }, isImage: function(type) {return type === Hoge.TypeImage; }, toggleImage: function($selector, $imageRow) { if (this.isText($selector.val())) {$imageRow.hide();} else {$imageRow.show();} }};! })(); ! App.nEdit = Edit; })(App); $(document).ready(function(){ ! var $checkedType = $('.typeSelector:checked'); ! var $imageRow = $('.questionImageRow'); ! $('.typeSelector').change(function(){ ! ! App.Edit.toggleQuestionImage($(this), $imageRow); ! }); ! App.Edit.toggleQuestionImage($checkedType, $imageRow); });
  4. PHPでJSを書く <?php $hoge = true; ?> <script> var hoge; if

    (‘<?php echo $hoge ?>’) { hoge = ‘hoge’; } else { hoge = ‘fuga’; } console.log(hoge); // hoge </script>
  5. コード var App = App || {}; (function(App){ // ςετͰ͖ͳ͍͠෼͔ΓͮΒ͍

    if ('<?php echo $hoge; ?>') ... else ... App.Bridge = { hoge: '<?php echo $hoge ?>', fuga: '<?php echo $fuga ?>' }; if (App.Bridge.hoge) ... else ... })(App);
  6. <script> // PHP͕ඞཁͳͱ͜Ζ͸HTML಺ʹهड़͢Δ App.Bridge = { hoge: '<?php echo $hoge

    ?>' }; </script> <script src=”app.js”></script> (function() { // PHPͱ෼཭Ͱ͖Δ if (App.Bridge.hoge) { // doSomething } })() HTML app.js
  7. var App = App || {}; (function(App){ // ςετͰ͖ͳ͍͠෼͔ΓͮΒ͍ if

    ('<?php echo $hoge; ?>') // doSomething else // doSomething App.Bridge = { hoge: '<?php echo $hoge ?>', fuga: '<?php echo $fuga ?>' }; if (App.Bridge.hoge) // doSomething else // doSomething })(App);