为什么这个:
(function () {
//code
}());
Run Code Online (Sandbox Code Playgroud)
还有这个:
var f = function () {
//code
}();
Run Code Online (Sandbox Code Playgroud)
工作,而这:
function () {
//code
}();
Run Code Online (Sandbox Code Playgroud)
才不是?它看起来完全一样 - 定义了匿名函数,并立即调用.有人可以从JavaScript/ECMAScript标准中引用它来解释这个吗?
更新:感谢大家的答案!所以这是关于函数表达式与函数声明的关系.请参阅此Stack Overflow答案,ECMAScript标准第13节,以及这篇精彩文章:命名函数表达式揭秘.
回顾一下答案:
第一个片段被解释为表达式,因为应用了分组运算符()- 请参阅ECMAScript标准第11.1.6节.
在第二个片段中,函数被解释为表达式,因为它位于赋值运算符的右侧部分=.
第三个片段没有任何允许解释器将函数作为表达式读取的东西,因此它被认为是一个声明,如果没有标识符则无效(Gecko允许它通过,但它会跟随下面的()分组操作符(因为它认为) )什么都不适用).
我有一个用户注册表单,email其中包含作为用户名的字段,并且应该在整个应用程序中是唯一的.
User model具有以下是该字段的验证规则:
var $validate = array(
'email' => array(
'email' => array('rule' => 'email', 'allowEmpty' => false, 'last' => true, 'message' => 'Valid email address required'),
'unique' => array('rule'=> 'isUnique', 'message' => 'Already exists'),
),
);
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我想检查它是否是'unique'失败的规则(显示不同的表单元素,如"发送密码恢复电子邮件"按钮).
我可以检查email字段是否有效(if (isset($this->User->validationErrors['email']))),但如何检查特定规则失败?
寻找特定的错误消息,就像 if ($this->User->validationErrors['email'] == "Already exists")是不正确(l10n等)...
我现在拥有的:
var result = $('selector1');
if (result.length == 0) result = $('selector2');
Run Code Online (Sandbox Code Playgroud)
但是这会打败链子.
问题是 - 如何使用JQuery链接获得相同的结果?
我不能使用$('selector1, selector2'),因为这总是会为两个选择器选择结果集,而我selector2只有在没有匹配元素时才需要结果selector1.
尝试将NLog实例初始化为静态类成员时,我得到一个奇怪的异常(更新:这发生在面向.NET 4.0的桌面应用程序中).
问题是,我只在一台特定的客户端计算机上获取它,并且无法在我的任何开发配置上重现.有人能指出我的方向,我应该寻找什么?
PS:用户也尝试使用管理员权限运行应用程序,获得相同的异常.
System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for nlog: Request for permission of type "System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" failed. (C:\Users\XXX\Desktop\Test.exe.Config line 9) ---> System.Security.SecurityException: Request for permission of type "System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" failed.
in System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
in System.Security.CodeAccessPermission.Demand()
in System.AppDomainSetup.VerifyDir(String dir, Boolean normalize)
in NLog.Internal.Fakeables.AppDomainWrapper..ctor(AppDomain appDomain)
in NLog.Internal.Fakeables.AppDomainWrapper.get_CurrentDomain()
in NLog.Config.ConfigSectionHandler.System.Configuration.IConfigurationSectionHandler.Create(Object parent, Object configContext, XmlNode section)
in System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader …Run Code Online (Sandbox Code Playgroud) 考虑以下实体模型:
public class Parent
{
public virtual FirstChild FirstChild { get; set; }
public virtual SecondChild SecondChild { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我加载了Parent实体:
Parent parent = <loaded in some way>;
Run Code Online (Sandbox Code Playgroud)
要显式加载其导航属性,我使用
db.Entry(parent).Reference(p => p.FirstChild).Load();
db.Entry(parent).Reference(p => p.SecondChild).Load();
Run Code Online (Sandbox Code Playgroud)
但这会导致两个数据库查询。
问题:有没有更优雅的方式,允许在单个查询中显式加载多个导航属性?
如果我没有parent加载,我会立即加载:
Parent parent = db.Parents
.Include(p => p.FirstChild)
.Include(p => p.SecondChild)
.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
但是,正如我所提到的,我已经在没有相关实体的情况下加载了它(并且我无法修改加载代码)。
下面代码的问题是:绑定到SomeClassProp.SubTextProp不起作用(源属性未设置为文本框内容),而绑定到TextProp它。
XAML:
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="wMain"
SizeToContent="WidthAndHeight">
<StackPanel>
<TextBox Text="{Binding ElementName=wMain, Path=SomeClassProp.SubTextProp}" Width="120" Height="23" />
<TextBox Text="{Binding ElementName=wMain, Path=TextProp}" Width="120" Height="23" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
和代码:
public partial class MainWindow : Window
{
public SomeClass SomeClassProp { get; set; }
public string TextProp { get; set; }
public MainWindow()
{
InitializeComponent();
SomeClassProp = new SomeClass();
}
}
public class SomeClass
{
public string SubTextProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了一些明显的东西吗?
请注意,我需要此绑定才能从目标(文本框)到源(类属性)工作。
更新:当我将绑定更改ElementName=wMain为RelativeSource={RelativeSource …
我需要对Product属于特定Category(HABTM关联)的s 列表进行分页.
在我的Product模型中,我有
var $actsAs = array('Containable');
var $hasAndBelongsToMany = array(
'Category' => array(
'joinTable' => 'products_categories'
)
);
Run Code Online (Sandbox Code Playgroud)
并在 ProductsController
$this->paginate = array(
'limit' => 20,
'order' => array('Product.name' => 'ASC'),
'contain' => array(
'Category' => array(
'conditions' => array(
'Category.id' => 3
)
)
)
);
$this->set('products', $this->paginate());
Run Code Online (Sandbox Code Playgroud)
但是,生成的SQL看起来像这样:
SELECT COUNT(*) AS `count`
FROM `products` AS `Product`
WHERE 1 = 1;
SELECT `Product`.`*`
FROM `products` AS `Product`
WHERE 1 = 1
ORDER …Run Code Online (Sandbox Code Playgroud) cakephp has-and-belongs-to-many paginate containable cakephp-1.3
考虑以下XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication1"
DataContext="{Binding Source={x:Static c:ViewModel.Instance}}"
>
<Grid>
<DataGrid DataContext="{Binding ItemsViewSource}" ItemsSource="{Binding}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
和视图模型:
public class ItemViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ViewModel
{
public static ViewModel Instance { get; set; }
static ViewModel()
{
Instance = new ViewModel();
}
public ObservableCollection<ItemViewModel> Items { get; private set; }
public CollectionViewSource ItemsViewSource { get; private set; }
public ViewModel()
{
Items = new ObservableCollection<ItemViewModel>(); …Run Code Online (Sandbox Code Playgroud) c# ×3
cakephp ×2
cakephp-1.3 ×2
data-binding ×2
javascript ×2
wpf ×2
.net-4.0 ×1
chaining ×1
containable ×1
datacontext ×1
datagrid ×1
elementname ×1
itemssource ×1
jquery ×1
nlog ×1
paginate ×1
xaml ×1