Closed kartik-v closed 10 years ago
I m working with grid in this days and a html::ajax can be a good think. Using grid with bootstrap modal and ajax need massive js and some method can help.
We already have Json
helper. We don't plan to support Html::ajax
etc. because these js code should better be done in js files and it's really easy to do this.
Yes, javascripts can be relegated to separate files and should be done so in most possible cases. There are however other cases when a lot part of javascript code (like ajax validations of inputs) is still required to be dynamically generated through server and may require additional coding if we do not have these helpers.
The alternative can also mean having a javascript (JS) library of helper functions that takes in server parameters for such cases. Idea is how can the developer minimize lines of code each time through reusable helper functions? I can try to put some use cases if needed. Sent from BlackBerry® on Airtel
-----Original Message----- From: Qiang Xue notifications@github.com Date: Thu, 21 Nov 2013 13:49:47 To: yiisoft/yii2yii2@noreply.github.com Reply-To: yiisoft/yii2 reply@reply.github.com Cc: Kartik Visweswarankartikv2@gmail.com Subject: Re: [yii2] Javascript Helpers (#1274)
We already have Json
helper. We don't plan to support Html::ajax
etc. because these js code should better be done in js files and it's really easy to do this.
Reply to this email directly or view it on GitHub: https://github.com/yiisoft/yii2/issues/1274#issuecomment-29028020
In Yii 1 I never use stuff like CHtml::ajax()
, but I all the time use CClientScript::registerScript()
and CClientScript::registerScriptFile()
. One doesn't always have a JS file to register/link to, and even when one does it's useful to be able to ask for it in multiple places but make sure it's only included once.
You only need Json::encode()
to encode server parameters. And we have the whole set of js code, file and bundle registration methods in View
.
You may refer to MaskedInput
to see how it deals with js.
@qiangxue - thanks that example helps and that certainly covers Html::ajax
and related stuff.
Now this makes me think about something beyond Yii1.1 (something like yii-helper.js
- containing functions for very frequently used javascript calls). Definitely need proper number of use cases (repeatable across) to cover this requirement. One use case very common is ajax validation of inputs in the onChange
event. For example, I can use functions like one below in multiple scenarios. Reduces errors in code and hastens programming (reducing lines of code).
/**
* yii-helper.js
*/
/* Default a variable if not defined */
function kvInit(arg, def) {
return (typeof arg == 'undefined' ? def : arg);
}
/* On change ajax client validation */
function kvValidateField(field, url, retval, target, msg, css) {
css = kvInit(css, 'loading center-top');
div = "#" + target;
$.ajax({
type: "POST",
dataType: "json",
url: url,
data: {retval: field.value},
beforeSend: function() {
$(div).html(msg);
$(div).addClass(css);
},
complete: function() {
$(div).removeClass(css);
},
success: function(data) {
$(div).html(data);
},
});
}
I understand the client script components being removed out of Yii2 inputs. But it maybe good if the Javascript helpers from Yii1.1 can be included for use in applications? Something like
Html::ajax
or basically utility functions fromCJavaScript
orCJSON
.