教程:如何创建一个MVC模式的组件(七) 添加model

添加一个 Model

序言

在上一文章中,我们做了一个Joomla 1.5 MVC模式的hello world 组件。

在例子中,greeting是直接写在view中的,这并不完全遵守MVC的原则,按照MVC的要求, view仅仅是展示数据而不是包含数据。

现在,我们来展示如何将数据迁移到Model中。接下来的例子中,你可以看到MVC设计模式的强大和灵活性。

创建Model

正如其名,model这个类是代表一些实体的。例子中,这个model代表 'hello'或者一个greeting。

model的命名约定是 {component namne}{Model}{model name},我们的例子中就是HelloModelHello

目前为止,hello model只有一个行为,就是返回greeting。因此只有一个方法,getGreeting(),这个方法仅仅返回字串‘Hello, World!’.


以下是model的代码:

<?php
/**
 * Hello Model for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class HelloModelHello extends JModel
{
    /**
    * Gets the greeting
    * @return string The greeting to be displayed to the user
    */
    function getGreeting()
    {
        return 'Hello, World!';
    }
}


先来看jimport( 'joomla.application.component.model' );这一行,Jimport函数的作用是载入组件必要的文件。
,也就是载入了/libraries/joomla/application/component/model.php,所有的加载文件都是相对于libraries目录。这个文件包含了JModel类的定义。


既然已经创建了model,我们必须修改view,从而view能从model获取数据。

使用model

Joomla! 自动调用与view相同名称的model,并推入view中. 因为view的名字是‘Hello’, 所以这个model会自动被加载.我们所要做的就是用JView::getModel()返回数据.

前例中:

$greeting = "Hello World!";

现在我们修改为如下代码:

$model =& $this->getModel();
$greeting = $model->getGreeting();

完整的view代码如下:

<?php

/**
 * Hello View for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */

class HelloViewHello extends JView
{
    function display($tpl = null)
    {
        $model =& $this->getModel();
        $greeting = $model->getGreeting();
        $this->assignRef( 'greeting', $greeting );

        parent::display($tpl);
    }
}
?>

添加文件到安装包中。
我们要做就是把这个hello.php加入在xml相应部分

<filename>models/hello.php</filename>

新的hello.xml完整清单如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/component-install.dtd">
<install type="component" version="1.5.0">
 <name>Hello</name>
    <!-- The following elements are optional and free of formatting conttraints -->
    <creationDate>2007 02 22</creationDate>
    <author>John Doe</author>
    <authorEmail> john.doe@example.org 为防备电子邮件地址收集器,这个 E-mail 地址被隐藏,你的浏览器必须支持 Javascript 才可看到这个邮件地址 </authorEmail>
    <authorUrl>http://www.example.org</authorUrl>
    <copyright>Copyright Info</copyright>
    <license>License Info</license>
    <!--  The version string is recorded in the components table -->
    <version>Component Version String</version>
    <!-- The description is optional and defaults to the name -->
    <description>Description of the component ...</description>

    <!-- Site Main File Copy Section -->
    <files folder="site">
        <filename>index.html</filename>
        <filename>hello.php</filename>
        <filename>controller.php</filename>
        <filename>views/index.html</filename>
        <filename>views/hello/index.html</filename>
        <filename>views/hello/view.html.php</filename>
        <filename>views/hello/tmpl/index.html</filename>
        <filename>views/hello/tmpl/default.php</filename>
        <filename>models/index.html</filename>
        <filename>models/hello.php</filename>
    </files>
   
    <administration>
        <!-- Administration Menu Section -->
        <menu>Hello World!</menu>
       
        <!-- Administration Main File Copy Section -->
        <!-- Note the folder attribute: This attribute describes the folder
            to copy FROM in the package to install therefore files copied
            in this section are copied from /admin/ in the package -->
        <files folder="admin">
    <!-- Site Main File Copy Section -->
            <filename>index.html</filename>
            <filename>admin.hello.php</filename>
        </files>       
    </administration>
</install>

posted on 2009-12-26 21:58  14的路  阅读(1428)  评论(0编辑  收藏  举报

导航

友情链接:源码下载