首页 > 活着 > YII AJAX registerScript

YII AJAX registerScript

1.YII封装的CHtml::ajaxLink,QuoteController.php

Java代码
1. 2.class QuoteController extends Controller
3.{
4. private $quotes = array(
5. array('Walking on water and developing software from a specification are easy if both are frozen.', 'Edward V Berard'),
6. array('It always takes longer than you expect, even when you take into account Hofstadter’s Law.', 'Hofstadter’sLaw'),
7. array('Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.','Rick Osborn'),
8. array('I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.', 'Bjarne Stroustrup'),
9. array('Java is to JavaScript what Car is to Carpet.', 'Chris Heilmann'),
10. );
11. private function getRandomQuote()
12. {
13. return $this->quotes[array_rand($this->quotes, 1)];
14. }
15. function actionIndex()
16. {
17. $this->render(‘index’, array(
18. ‘quote’ => $this->getRandomQuote()
19. ));
20. }
21. function actionGetQuote()
22. {
23. $this->renderPartial(‘_quote’, array(
24. ‘quote’ => $this->getRandomQuote(),
25. ));
26. }
27.}
class QuoteController extends Controller
{
private $quotes = array(
array('Walking on water and developing software from a specification are easy if both are frozen.', 'Edward V Berard'),
array('It always takes longer than you expect, even when you take into account Hofstadter’s Law.', 'Hofstadter’sLaw'),
array('Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.','Rick Osborn'),
array('I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.', 'Bjarne Stroustrup'),
array('Java is to JavaScript what Car is to Carpet.', 'Chris Heilmann'),
);
private function getRandomQuote()
{
return $this->quotes[array_rand($this->quotes, 1)];
}
function actionIndex()
{
$this->render(‘index’, array(
‘quote’ => $this->getRandomQuote()
));
}
function actionGetQuote()
{
$this->renderPartial(‘_quote’, array(
‘quote’ => $this->getRandomQuote(),
));
}
}view/index.php

Java代码
1.

Quote of the day

2.

3.“”,
4.

5.clientScript->registerCoreScript(‘jquery’); ?>
6. 7.array('update' => ‘#quote-of-the-day’))?>

Quote of the day

”,

clientScript->registerCoreScript(‘jquery’); ?>
array('update' => ‘#quote-of-the-day’))?> ajaxLink($text,$url ,$ajaxOptions=array(),$htmlOptions=array())

$text 链接内容

以下是调试.也可以用常用的jQuery.ajax

Java代码
1. 2.array('success' => ‘js:function(data){
3.alert(data);
4.}’))?>
array('success' => ‘js:function(data){
alert(data);
}’))?>

2.将php数据转化成javascript数据

protected/config/main.php:

Java代码
1.’params’=>array(
2. // this is used in contact page
3. ‘adminEmail’=>’webmaster@example.com’,
4. ‘alert’ => array(
5. ‘enabled’ => true,
6. ‘message’ => ‘Hello there!’,
7. ),
8.),
‘params’=>array(
// this is used in contact page
‘adminEmail’=>’webmaster@example.com’,
‘alert’ => array(
‘enabled’ => true,
‘message’ => ‘Hello there!’,
),
),protected/controllers/AlertController.php

Java代码
1. 2.class AlertController extends Controller
3.{
4. function actionIndex()
5. {
6. $config = CJavaScript::encode(Yii::app()->params->toArray());
7. //$config:{‘adminEmail':’webmaster@example.com’,’alert':{‘enabled':true,’message':’Hello there!’}}
8. Yii::app()->clientScript->registerScript(‘appConfig’, “var config = “.$config.”;”,CClientScript::POS_HEAD);
9. $this->render(‘index’);
10. }
11.}
class AlertController extends Controller
{
function actionIndex()
{
$config = CJavaScript::encode(Yii::app()->params->toArray());
//$config:{‘adminEmail':’webmaster@example.com’,’alert':{‘enabled':true,’message':’Hello there!’}}
Yii::app()->clientScript->registerScript(‘appConfig’, “var config = “.$config.”;”,CClientScript::POS_HEAD);
$this->render(‘index’);
}
}registerScript第二个参数是显示js代码。

Java代码
1. 2.Yii::app()->clientScript->registerScript(‘search’, ”
3.$(‘.search-button’).click(function(){
4. $(‘.search-form’).toggle();
5. return false;
6.});
7.$(‘.search-form form’).submit(function(){
8. $.fn.yiiGridView.update(‘project-grid’, {
9. data: $(this).serialize()
10. });
11. return false;
12.});
13.”);
14.?>
Yii::app()->clientScript->registerScript(‘search’, ”
$(‘.search-button’).click(function(){
$(‘.search-form’).toggle();
return false;
});
$(‘.search-form form’).submit(function(){
$.fn.yiiGridView.update(‘project-grid’, {
data: $(this).serialize()
});
return false;
});
“);
?>protected/views/alert/index.php

Java代码
1.
CJSON::encode()=CJavaScript::encode 生成json格式
CJSON::decode()

1.YII封装的CHtml::ajaxLink,QuoteController.php Java代码 1.&r […]

  1. 还没有评论
评论提交中, 请稍候...

留言

(Spamcheck Enabled)

Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks