I have written the following piece of code in my view page
<script type="text/javascript">
function filldetails()
{
document.getElementById('FirstName').value = "hjshjsh";
}
</script>
echo $this->Form->select('students',$student_name,array('onchange' =>filldetails()));
but i am getting an error message
call to undefined function filldetails()
How do I solve this error?
it should be 'onchange' => 'filldetails()'
Unless your project has a specific reason to avoid JS frameworks, you will avoid a lot of long-term headaches by using jQuery instead of pure Javascript.
Rewrite your view thus:
<?php
$selectDomId = $this->Form->domId('students');
$firstnameDomId = $this->Form->domId('Student.first_name');
$this->Html->scriptBlock("
jQuery(function($){
$('#{$selectDomId}').change(function(event){
$('#{$firstnameDomId}').val( $(this).val() );
});
});
",array('inline'=>false));
?>
<?php echo $this->Form->select('students',$student_name,$this->Form->domId(array(),'students'))?>
<?php echo $this->Form->input('Student.first_name')?>
The jQuery takes care of the onChange event handler, so it doesn't clutter your HTML, by hooking onto your dropdown menu's change event.
The use of Helper::domId means you don't have to worry about how CakePHP's helpers generate their id attributes, which is a net win in reliability and maintainability.
Related
Is it possible to get the response object sent by the render() (Symfony2) method?
An example:
public function indexAction(){
$params = array('Hi' =>'Hello world', 'userName' =>'Jack');
return $this->render('exampleBundle:Default:index.html.twig', $params);
}
I'm totally able to capture the var $params and its content in the html.twig file, but I can't figure out how to get that content it in javascript to render the view using angular. Just in case, I tried many absurd things, but they didn't work at all obviously.
Maybe it isn't possible at all and I'll need to redesign it or make a new ajax call once the document is loaded instead of passing the content via the render() method. I'm not really sure so, can I reach this without make an ajax request?
This approach seems to me a bad thing. Most of modern client applications philosophy is to to load as soon as possible the web page from server and the load data through xhr request.
Said that if you need or want to do it, you can achieve it in differents manners:
I did not tried the examples, so this can contains some syntax errors:
1- JS Vanilla approach; in your html.twig
<script type="text/javascript">
window.nameSpaceOfMyApp = window.nameSpaceOfMyApp || {};
window.nameSpaceOfMyApp.exchange = window.nameSpaceOfMyApp.exchange || {};
window.nameSpaceOfMyApp.exchange.params = JSON.parse({{$params |json_encode()}});
</script>
2- More "Angular Way" Not overloading global namespace and better testing...If you have load angular.js at this point you can create in twig template the main module of your angular App (if you cant use your main module, you can use a new module and require it as dependency of your main module)
in your html.twig:
<script type="text/javascript">
angular.module('moduleName',[<dependencies>,...])
.value('exchange',{
params: JSON.parse({{$params |json_encode()}})
})
</script>
Later you can inject this value where you need it.
angular.module('moduleName').controller('nameOfController',['exchange', function(exchange){
console.log(exchange.params)
}]);
I have followed tutorials such as this one and this one. At first glance they are quite simple as in how to add JQuery UI to Drupal 7. Nonetheless, when I apply them, all I get is that the function dialog is undefined. Here is what I have done:
template.php
function mytheme_preprocess_image(&$variables) {
drupal_add_library('system','ui.dialog');
}
chat_overlay.js
jQuery(document).ready(function(){
console.log("*********************Setting up click");
jQuery('#small_chat_wrapper #chat_message').click(function(){
console.log("*********************CLICKED CLICKED");
if (typeof jQuery.ui !== 'undefined')
console.log("loaded");
else
console.log("undefined");
jQuery('#chat_overlay').dialog('open');
});
})
The console logs print out wonderfully, and the if check prints out loaded. Nonetheless, when it gets to the line of .dialog(..) the Undefined error occurs. I don't know what else to try. Such a simple thing and it's becoming painful.
Help is very much appreciated.
Edit:
The site's Status Report has the following:
jQuery Update jQuery 1.7.1 and jQuery UI 1.8.7
My MISTAKE:
Change function mytheme_preprocess_image(&$variables) to function mytheme_preprocess_page(&$variables) . Goodness.
Now the UNDEFINED ERROR NO LONGER happens, BUT, even though the ui classes and divs are getting added, the UI CSS is not coming through (the dialog does not pop up).
Really would appreciate some help.
You should load the jquery library that you need either in the preprocess_html function of your template.php or in yourmodule_init hook like that:
<?php
function yourmodule_init(){
drupal_add_library('system', 'ui.dialog');
drupal_add_library('system', 'ui.draggable');
drupal_add_library('system', 'ui.sortable');
}
?>
The source: https://www.drupal.org/node/1172846
I'm working with a facebook tab. It's an iframe posts will recieve certain in the http-response only if the page is embedded in an iframe. This means that I can't use $http as I normally do. I try to figure out how to supply this data to angular.
I tried this, but it doesn't work.
<?php
if($signed_request['page']['liked'] == 1){
echo '<script type="text/javascript">$scope.liking = true</script>';
}else{
echo '<script type="text/javascript">$scope.liking = false</script>';
}
?>
I have considered adding some sort of html-field, like an input type="hidden" with a ng-model attatched to it, but it feels kind of hacky. Does angular have any suggested approach to solve this kind of problem?
I make a project in worklight used dojo mobile 1.8.1 and angularjs 1.0.1,but i got a strange problem.
Here is my html part:
<div data-dojo-type="dojox.mobile.ScrollableView" data-dojo-props="selected:true" id="id1" ></div>
<div class="full" data-dojo-type="dojox.mobile.View" id="id2"></div>
and my JavaScript part:
require([
"dojo", "dijit/_base/manager","dojo/parser", "dijit/registry",
], function(dojo) {
dojo.ready(function() {
// dijit.byId("id1").performTransition("id2"); //////////place I
});
});
var angularApp = angular.module('app', [])
.run(['$rootScope','$templateCache','$route',
function($rootScope,$templateCache,$route) {
dijit.byId("id1").performTransition("id2");////////place II
}]);
The problem is at place I, it works well, but when I put "dijit.byId("id1")" at place II, it shows:
dijit.byId("").is not defined
The ready function is executed after dojo parsed your document & constructed the widgets you try to get using dijit.byId.
The second part is not placed within the ready function, so dojo can't find your elements yet !
Solution: Access your elements in the ready function OR do not declare them declaratively (like you did, using html code...) !
Lucian
The dojo.ready() function registers an event-handler function (callback) which will be fired after the DOM got completely parsed.
This comes in very handy if you want to be sure that every html element got rerendered as dojo-widget before you perform operations on them.
So, in your example, Code II will be executed before the dijit.byId() function has been made available by loading the necessary modules (dijit/registry, ...). Code II would only work after the dom-ready event got fired and your "dojo.ready()" function did load the required modules.
You should definately invest 5 minutes in reading what dojo.ready() is about:
http://dojotoolkit.org/reference-guide/1.8/dojo/ready.html
Sidenote:
You shouldn't use dijit.byId() any more in Dojo 1.8.1. Try using dijit.registry.byId() (you have to require the dijit/registry module).
http://dojotoolkit.org/reference-guide/1.8/dijit/registry.html#dijit-registry-byid
How to give language strings in js while using cakephp (Im using cakephp version 2) ? I know, this can be done in the following way.
<script>
var LABEL_LOGIN = '<?php echo __('Login'); ?>';
</script>
I want to separate my js code from my view file. Anything wrong if I do the same in .js file (because I am using global vars) ? Is there any other good solution to apply multilanguage in js ?
Cake does not support i18n in JS out of the box. I would rather recommend pulling in some i18n JS plugin which is syntactically compatible, i.e. also uses a global __ function.
Then you coul use the same method to parse all source codes for i18n keys.
The only easier way i could think of is:
Create some element, say langs.ctp, and add your global js vars in there, like
<script type="text/javascript">
var LABEL_LOGIN = '<?php echo __('Login'); ?>';
var LABEL_LOGOUT = '<?php echo __('Logout'); ?>'; // and so on
</script>
and then load this element in your layout, like, in your layout inside head tag
<?php echo $this->element("langs");
then you could use your global js vars
Hope it helps