dua55014 2011-06-23 03:56
浏览 29

是否有一种标准的方式来处理仅在MVC中的一个视图中使用的javascript?

This is the first time I've tried MVC. I am using Code Igniter for the MVC along with jQuery and jQueryUI for the front end. I have the basic idea of MVC and I have a working site. Conceptually, though, I want to make sure that I stay as rigid as possible with respect to the MVC idea.

I have a main skin view and then use load->view("skin", $contents) where $contents is an associative array containing other views.

As a result, I have one page (called "admin Console") in which the skin view is called and $content contains more HTML. I want a jQueryUI dialog box to be used only on that page. To create this, I have a <script> tag that contains the dialog box javascript in the admin console view. This ends up with a <script> tag in the middle of a bunch of HTML that includes when the final page is rendered.

The end result of this is that I am lazy loading the javascript but it may result in multiple jQuery $(document).ready(function(){...}); calls.

I have everything working, but I am not sure if my implementation is true to the MVC principles. Is it better to use the method I am using in which the javascript is only loaded when it's being used or should I put the javascript somewhere else? Am I making mountains out of mole hills?


My Relevant Controller Code:

function index(){
$tree = $this->mainModel->getUnitTreeMeta();
$treeOutput = $this->load->view('unit_tree', array('units' => $tree), TRUE);
$pages = $this->mainModel->getPages();

$console = $this->load->view('admin_console', array('units'=>$tree, 'pages'=>$pages), TRUE);

$content = array(
    'title'         =>  'Admin Console',
    'css'           =>  link_tag(array('href' => 'css/admin.css', 'rel' => 'stylesheet', 'type' => 'text/css', 'media'=> 'screen'))."
",
    'navbar'        =>  'navbar',
    'content'       =>  $console,
    'rightSidebar'  =>  $treeOutput,
    'footer'        =>  ''
);

$this->load->view('skin', $content);
}

skin.php contains the line <?=$content?> which is the HTML/JS contained in the admin_console view:

<div>
    <div>
        ...
        <a href="" id="addUnit" class="smallLinks">Add a New Unit</a>
    </div>
</div>

<!-- DIALOG BOXES -->
<div id="addUnitDialog" title="Add A New Unit">
    <?php echo form_open('admin/add_unit'); ?>
    Unit Name: <br />
    <?php echo form_input("unitName", "");?>
    <br />
    Content: <br />
    <?php echo form_textarea("content", "");?>
    <?php echo  form_close();?>
    <div id="addUnitResponse"></div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $( "#addUnitDialog" ).dialog({
            autoOpen: false,
            minWidth: 680,
            buttons: {
                "add Unit" : function(){
                    var unitName = $("input", this).val();
                    var content = $("textarea", this).val();

                    $.ajax({
                        url : "add_unit",
                        type : "POST",
                        success :function(){
                            location.reload();
                        },
                        error : function(){
                            $("#addUnitResponse").text("error");
                        }
                    });
                }
            }           
        });
        $("a#addUnit").click(function(){
            $( "#addUnitDialog" ).dialog( "open" );
            return false;       
        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • doubeng1278 2011-06-23 05:50
    关注

    Personally, I have backed off from my opinion that JS should never, ever appear in HTML. But, the way I've normally done this type of thing is include a "if(isset(..." block at the <head> of the view:

    // You may choose to have non-array implementations...
    if(isset($js_array))
    {
        foreach($js_array as $file)
        {
            if(strpos($file, '.'))
                 echo "<script type=\"text/javascript\" src=\"$file\" />" . PHP_EOL;
            else
                 echo "<script type=\"text/javascript\">$file</script>" . PHP_EOL;
        }
    }
    

    The drawback here is that it requires the control to know about how the view is rendering different things. Of course, that could be fixed by creating a file, admin.php which has two lines in it:

     $js_array = array('/custom_js.js');
     require_once('skin.php'); //<-- will cause issues if your server does not 
                               //    allow short tags, but CodeIgniter is parsing 
                               //    them for you. (I find that bad anyway...)
    

    You'd need to call $this->load->view('admin', $content);, but that is a minor change.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集