I am working on the basic template of Yii2. I have got a jQuery script views/usuario/js/create.js that it's only going to be used in one view views/usuario/create.php.
I'd prefer not to use ...
public $jsOptions = array(
'position' => \yii\web\View::POS_HEAD
);
... in assets/AppAsset.php in order to mantain loading of scripts at the end of the page.
create.js it's only needed in the create.php view so I'd prefer to load it just for the create.php view.
So I've tried unsuccessfuly to follow ippi's instructions
Firebug keeps throwing me this error:
SyntaxError: expected expression, got '<'
http://www.example.com/usuario/js/create.js
Line 1
I guess there could be a problem with the route param of ...
$this->registerJsFile('js/create.js');
... but I can't find it out.
Any help would be appreciated.
registerJsFile() needs an url, you should simply publish (make it web accessible) your file before registering it, e.g. :
$pub = Yii::$app->assetManager->publish(__DIR__ . '/create.js');
$this->registerJsFile($pub[1], ['depends' => ['yii\web\JqueryAsset']]);
Or you could create an asset bundle and registering it in your view.
Read more about publish() and registerJsFile().
Try to use $this->registerScriptFile('views/usuario/js/create.js');
Related
I am using elFinder to manage assets for a web site and it's default functionality works great; however I need to add some additional logic to several of the PHP functions which resides in on of my Controllers.
The place that I would like the logic to be added is <elfinder_Dir>/PHP/elFinderVolumeLocalFileSystem.php specifically in the function _unlink($path) before a file is actually removed I would like to call out to another class to destroy the database entries for this asset.
The original function looks like this:
protected function _unlink($path) {
return #unlink($path);
}
When I try to add code like the following:
protected function _unlink($path) {
var_dump($path);
return #unlink($path);
}
OR
//top of file...
use controllers\ResourceManager;
//OR
//include <pathToResourceManager>
//...
protected function _unlink($path) {
ResourceManager::delteFromDB();
return #unlink($path);
}
I receive this alert on the screen:
I also noticed that when that message is given, the header in my "Network" tab shows a Response Header Content-type of text/html instead of application/json as expected by the JS portion of elFinder.
Why is the header Content-type being changed when I add custom logic?
Is there a better way to add this functionality to the project?
The answer to my question turned out to be pretty straight forward.
1) error_reporting(0); was squashing all of my errors related to using the proper namespace-ing for my files, I changed this to error_reporting(E_ALL) so I could see the real problem.
2) The files needed to be added to a namespace, since I used the same namespace I did not have any extra include_once() calls.
Next I had to add replace this line:
$class = 'elFinderVolume'.(isset($o['driver']) ? $o['driver'] : '');
With:
$class = __NAMESPACE__ . '\\elFinderVolume'.(isset($o['driver']) ? $o['driver'] : '');
Which allows the driver (which is now in the namespace) to be loaded properly.
Once these changes were made, all is well, I can add my own logic where I please.
I am creating a custom profile page for one of my WP plugins, where I want the user to have a regular page instead of seeing the WP Dashboard. All works fine so far, except the script for the password-strength. When I start typing the password, I get this in my Firebug console:
TypeError: blacklist is undefined
blacklist = [ blacklist.toString() ];
Which is thrown when the "meter" event is called at the beginning:
meter : function( password1, blacklist, password2 ) {
if ( ! $.isArray( blacklist ) )
blacklist = [ blacklist.toString() ];
I have no idea what it's working fine in the WP dashboard. I spend about an hour searching, then I decided to use a workaround. If anybody could point me into the right direction, what I am missing here, I would appreciate that.
My workaround for now is to put this into a try/catch block:
meter : function( password1, blacklist, password2 ) {
try {
if ( ! $.isArray( blacklist ) )
blacklist = [ blacklist.toString() ];
}
catch (e){}
Which works fine now, even when I use it on the WP front end. But it's not an optimal solution, as I have to copy the JS files password-strenght-meter.js to my own plugin and enqueue it from there. Since I also need to use the user-profile.js, I also have to copy this too, so I need to duplicate these two JS files which is not a good solution.
Any ideas, why jQuery is stopping the script when used on a front end page, while it works fine on the admin backend?
==== EDIT ========
I just found that the problem is related to another plugin "WP User Frontend" which seems to override the password-strenght-meter script with their own code. So I think I will be able to fix this now.
Sorry for the misleading question. I have checked that this problem was related to having another plugin "WP User Frontent" activated, that did interfere with the existing admin scripts from WordPress. So here is how I solved it.
First of all, I put this into a custom page template, so this needs to be placed in the theme directory:
<?php
/**
* Template Name: User Profile Dashboard
*/
// remove other conflicting scripts
if (class_exists('WPUF_Main'))
{
global $wpuf;
remove_action( 'wp_enqueue_scripts', array($wpuf, 'enqueue_scripts') );
}
get_header();
wp_enqueue_script('user-profile');
wp_enqueue_style('wp-admin');
include_once(ABSPATH . '/wp-admin/includes/template.php');
... other code that handles the user profile fields ...
<?php get_footer() ?>
The tricky part was to get rid of the already enqueued scripts that came from the WPUF_Main class. It seems that WP User Frontend instantiates the class into a global variable $wpuf when the plugin is active, so I had to use this variable to remove the action for enqueuing the scripts. Then I enqueued the scripts and styles for user-profile and wp-admin, and included template.php from wp-admin, because I needed the function submit_button() which is declared here.
I hope this will help others who come up with the same problem, because the combination of WP User Frontend and having a custom Profile page, does make sense, when the webmaster don't want to have their users to see the WP Dashboard at all.
I have a view that renders some data in a CListView and a Highcharts graphic.
Please note that the pager of the CListView works the following way: it requests the "new page" to the server, the servers renders the entire content of the page and sends it to the CListView. The body is then replaced with the content that is being sent, with something like this: $('body').html(rendered_html_response).
I also have a dropDownList that imitates this behavior. This is the code:
<?php
echo CHtml::dropDownList('usage', 'cg', array(
'd'=>'Daily',
'm'=>'Monhly'
), array(
'ajax'=>array(
'type'=>'POST',
'url'=>$this->createUrl('admin/user', array(
'id'=>$user->id)
),
'update'=>'body',
'data'=>array('cg'=>'js:$(this).val()'),
'options'=>array(
Yii::app()->session['cg']=>array(
'selected'=>true
)
)
)
));
?>
Anyways, there is a problem. When I use the CListView, everything works fine (as in: the page's content is refreshed without the page being reloaded, the charts and the data are rendered correctly, etc...), but when I select something using the dropDownList, the server renders the reply, sends it to the client, the client starts replacing the content of body, but then I get this error: http://www.highcharts.com/errors/16.
I tried disabling highcharts.src.js this way:
public function beforeAction(){
if(Yii::app()->request->isAjaxRequest){
Yii::app()->clientScript->scriptMap['highcharts.src.js'] = false;
}
return true;
}
but then I get the typical undefined javascript error (means that something is trying to use Highcharts, which is not defined).
Where is the problem and how can I fix it?
//init
$('#container').highcharts({});
//select onchange
$('#container').highcharts().destroy();
As following the error link says
Highcharts already defined in the page
This error happens the second time Highcharts or Highstock is loaded in the same page, so the Highcharts namespace is already defined. Keep in mind that the Highcharts.Chart constructor and all features of Highcharts are included in Highstock, so if you are running Chart and StockChart in combination, you only need to load the highstock.js file.
You are loading the highcharts javascript twice. You can try
a) enabling highcharts on the first load and then disabling it on the next e.g perhaps checking if ajax
b) use custom javascript to check whether highcharts has been loaded as in this blog post
c) use the NLSClientScript extension that handles js and css loading for you
I suggest you use the last one. I was bitten by this problem a couple of times until I discovered it. It eliminates the need to single out each javascript function/file.
EDIT
Instead of editing the listview js, you can edit your code to use the same update function:
<?php
echo CHtml::dropDownList('usage', 'cg', array(
'd'=>'Daily',
'm'=>'Monhly'
), array(
'onChange'=>'$.fn.yiiListView.update("your-list-id", {
data: {"usage":$(this).val()},
type: "POST"
});'
));
?>
Hi I am just starting a tutorial in javascriptMVC (JqueryMX) I have an error "Uncaught type error Cannot call method 'Model' of undefined" I have checked all path and don't understand why it is still not working. Any help will be greatfull. todo.js below
steal('jquerypp/class',
'jquerypp/controller',
'jquerypp/model',
'jquerypp/view/ejs',
'jquerypp/dom/fixture',
function($){
$.Model('Todo',
{
findAll : "GET /todos",
findOne : "GET /todos/{id}",
create : "POST /todos",
update : "PUT /todos/{id}",
destroy : "DELETE /todos/{id}"
},{})
});
did you have this solved already?
if not, i would suggest that you try the JMVC code generator (./js jmvc/generate/app myapp) as seen in here --> http://javascriptmvc.com/, there you will see how the libraries are placed.
and by the way, the steal.js should be placed in the html file, all others can be in the main js file.
I'm trying to find the best way to deal with dynamic routing generated through an AJAX call with Symfony2.
When a new call is made, I need the current path to be available , along with some dynamic variables that get passed into the path.
Essentially this .
A few answers have suggested putting the route into a variable within each templete , such as
<script type="text/javascript">
var productPath = {{ path("acme_myBundle_default_product" , {"magazine" : "bobscheese" , "product" : "chedderfornoobs"}) }};
</script>
The issue here is, the path rely s on variables, that won't exist at runtime (namely $magazine and $product).
A perfect solution would be FOSJsRoutingBundle it seems , but the installation doesn't seem to be up to date with the latest Symfony2 .
Installation runs fine with git submodule add git://github.com/FriendsOfSymfony/FOSJsRoutingBundle.git vendor/bundles/FOS/JsRoutingBundle
but then I think the rest of the ReadMe is out of date, following it gives me a blank screen, with no errors in the log.
So my question is , either , how to install FOSJsRoutingBundle in Symfony2.1.3 , or how best to handle client side generated URLS within Symfony2.
FOSJsRoutingBundle can be normally used in my environment(2.1.3).
Does routing go wrong?
Has it set up?
acme_myBundle_default_product:
pattern: // ...
defaults: // ...
options:
expose: true
I just went down the
<script type="text/javascript">
var basePath = 'http://www.mybaseurl.com';
</script>
Route. Not as fulfilling, but worked for me in this case.