小编lxa*_*lxa的帖子

JavaScript匿名函数立即调用/执行(表达式与声明)

可能的重复:
JavaScript中的函数表达式与声明之间有什么区别?
解释JavaScript封装的匿名函数语法

为什么这个:

(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节,以及这篇精彩文章:命名函数表达式揭秘.

回顾一下答案:

  1. 第一个片段被解释为表达式,因为应用了分组运算符()- 请参阅ECMAScript标准第11.1.6节.

  2. 在第二个片段中,函数被解释为表达式,因为它位于赋值运算符的右侧部分=.

  3. 第三个片段没有任何允许解释器将函数作为表达式读取的东西,因此它被认为是一个声明,如果没有标识符则无效(Gecko允许它通过,但它会跟随下面的()分组操作符(因为它认为) )什么都不适用).

javascript anonymous-function

58
推荐指数
1
解决办法
2万
查看次数

找出(在控制器中)哪个特定验证规则失败

我有一个用户注册表单,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等)...

cakephp cakephp-1.3

6
推荐指数
2
解决办法
2747
查看次数

如何使用OR条件链接选择器(如果main为空,则替换结果集)

我现在拥有的:

var result = $('selector1');
if (result.length == 0) result = $('selector2');
Run Code Online (Sandbox Code Playgroud)

但是这会打败链子.

问题是 - 如何使用JQuery链接获得相同的结果?

我不能使用$('selector1, selector2'),因为这总是会为两个选择器选择结果集,而我selector2只有在没有匹配元素时才需要结果selector1.

javascript jquery jquery-selectors chaining

6
推荐指数
1
解决办法
578
查看次数

ConfigurationErrorsException/SecurityException/FileIOPermission在NLog实例化期间读取应用程序.config文件

尝试将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)

c# configuration securityexception nlog .net-4.0

5
推荐指数
1
解决办法
3819
查看次数

在实体上显式加载多个引用/集合

考虑以下实体模型:

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)

但是,正如我所提到的,我已经在没有相关实体的情况下加载了它(并且我无法修改加载代码)。

c# entity-framework eager-loading navigational-properties

5
推荐指数
1
解决办法
1737
查看次数

RelativeSource 适用于(嵌套)子属性,而 ElementName 不适用

下面代码的问题是:绑定到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 …

c# data-binding wpf xaml elementname

4
推荐指数
1
解决办法
1217
查看次数

使用Containable通过相关模型(HABTM)上的条件过滤分页结果

我需要对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

2
推荐指数
1
解决办法
6991
查看次数

WPF将DataGrid绑定到CollectionViewSource:通过DataContext工作,通过ItemsSource清空; 区别?

考虑以下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)

data-binding wpf datacontext datagrid itemssource

1
推荐指数
1
解决办法
2814
查看次数