我有一个 View ,我需要在标题中调用 JQuery 以及另一个 JS 库。对于所有其他页面,它们可以在文档末尾呈现。在 Yii2 中实现此目标的最佳方法是什么?
目前我只是在我的 AppAsset.php 中这样做
public $js = [
'https://code.highcharts.com/highcharts.js',
];
public $jsOptions = ['position' => \yii\web\View::POS_HEAD];
我看过制作 new Asset , 但它似乎没有用,我不确定这是否是正确的方法。因为我不想在每个页面中首先呈现 JS。
<?php
namespace app\assets;
use yii\web\AssetBundle;
class EarlyJavaScriptAsset extends AssetBundle
{
public $js = [
'https://code.highcharts.com/highcharts.js',
];
public $jsOptions = ['position' => \yii\web\View::POS_HEAD];
public $publishOptions = [
'only' => [
'report/report-1', // this is view folder location
]
];
}
最佳答案
如果你想看看 AssetBundle::register()你会看到它使用 View::registerAssetBundle()没有第二个参数。但是第二个参数是你需要的东西:
@param integer|null $position if set, this forces a minimum position for javascript files. This will adjust depending assets javascript file position or fail if requirement can not be met. If this is null, asset bundles position settings will not be changed. See registerJsFile for more details on javascript position.
在你看来,你注册你的 Assets 包的地方,改变
EarlyJavaScriptAsset::register($this);
到
$this->registerAssetBundle(EarlyJavaScriptAsset::className(), $this::POS_HEAD);
和
$this->registerAssetBundle(EarlyJavaScriptAsset::className(), $this::POS_END);
第二视角
PS: 当然 $this 是你的 View 的上下文。
关于php - 有条件的 AppAssets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34881259/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS
除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p
我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我
基本上,我只是试图在满足特定条件时停止程序运行其余行。unlessraw_information.firstputs"Noresultswerereturnedforthatquery"breakend然而,在程序运行之前我得到了这个错误:Invalidbreakcompileerror(SyntaxError)执行此操作的正确方法是什么? 最佳答案 abort("Noresultswerereturnedforthatquery")unlesscondition或unlessconditionabort("Noresultswer
如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:
我有一个简单的问题,但我无法解决这个问题。我的字符串格式为ID:dddd,具有以下正则表达式:/^ID:([a-z0-9]*)$/或者如下:ID:1234Status:232,所以用下面的正则表达式:/^ID:([a-z0-9]*)Status:([a-z0-9]*)$/现在我想制作一个可以处理两者的正则表达式。我想到的第一件事是:/^ID:([a-z0-9]*)$|^ID:([a-z0-9]*)Status:([a-z0-9]*)$/它匹配,但我正在研究条件正则表达式,并认为应该可以按照(伪代码)ifthestringcontains/Status://^ID:([a-z0-9]*)
我正在做一个项目。目前我有一个相当大的条件语句,它根据一些输入参数为变量赋值。所以,我有这样的东西。ifsomeconditionx=somevalueelsifanotherconditionx=adifferentvalue...重构它的最佳方法是什么?我希望我最终会得到类似的东西x=somevalueifsomecondition||anothervalueifanothercondition这种事情有规律吗? 最佳答案 只需将赋值放在if之外即可。x=ifsomeconditionsomevalueelsifanotherc