小编cry*_*yss的帖子

奇怪的JavaScript语法我以前从未使用过,但它的工作原理

你知道为什么这个代码编译,是什么something

function Box() {
  something: {
    alert(1);
  }
}

var box = new Box();
Run Code Online (Sandbox Code Playgroud)

javascript

8
推荐指数
2
解决办法
140
查看次数

仅当destination属性不为null时才使用UseDestinationValue

当我想使用UseDestinationValue方法中的行为时,如何配置AutoMapper映射,但仅当目标属性为NOT时才配置AutoMapper映射null.

像这样的东西:

Mapper.CreateMap<Item, ItemViewModel>()
    .ForMember(x => x.Details, _ => _.UseDestinationValue(dontUseWhenNullDestination: true))
Run Code Online (Sandbox Code Playgroud)

编辑

class ItemDetails {
    public string Info { get; set; }
    public string ImportantData { get; set; } // only in Domain, not in ViewModel
}

class Item {
    public ItemDetails Details { get; set; }
}

class ItemDetailsViewModel {
    public string Info { get; set; }
}

class ItemViewModel {
    public ItemDetailsViewModel Details { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在使用的例子.我有一个ItemViewModel班级,我想把它映射到Item班级.

映射配置: …

.net c# automapper

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

如何从 KnockoutJS 中的组件节点获取组件的视图模型

我有一个名为“我的组件”的组件:

ko.components.register('my-component', { 

    viewModel: function() {      
        return { title: 'title' };     
    },

    template: '<div>x</div>'

});
Run Code Online (Sandbox Code Playgroud)

我在这样的视图中使用这个组件:

<my-component params=""></my-component>
Run Code Online (Sandbox Code Playgroud)

有没有办法让组件的视图模型与这个 HTML 节点相关?ko.dataFor 不起作用:

ko.dataFor($('my-component').get(0)) // it doesn't return component's view model
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

javascript knockout.js

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

在后台运行异步操作方法

有没有办法在标准操作方法中调用异步操作方法而不是等待异步方法执行(保持相同的Request对象)?

public class StandardController : Controller
{
    public ActionResult Save()
    {
        // call Background.Save, do not wait for it and go to the next line

        return View();
    }
}

public class BackgroundController : AsyncController
{
    public void SaveAsync()
    {
        // background work
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用Task类来执行backround工作,但是当我启动任务并且action方法返回了View时,请求被杀死并且我的DependencyResolver实例被删除,因此后台任务开始抛出异常.

第一个想法是执行Standard.Save(不调用后台任务)并返回View,其中可以在ajax中调用Background.Save方法.换句话说:将另一个请求调用到异步控制器,以启动后台任务.

主要问题是如何调用异步方法保留授权信息(在cookie中)和依赖解析器(在我的例子中:autofac).

c# asp.net-mvc asynchronous asp.net-mvc-4

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

使用 x 和 y 位置设置文本区域的插入符位置

我想实现内联版本,因此如果用户单击带有某些文本的 div,文本区域将出现在与单击的 div 位置相同的位置,并从 div 获取文本。这对我来说很好,但我接下来要做的是根据 div 单击事件的 x 和 y 位置设置 textarea 的插入符位置。有任何想法吗?

HTML:

<div id="content" style="display: block; z-index: 10">Some text</div>

<textarea id="editor" style="position: absolute; display: none; z-index: 11"></textarea>
Run Code Online (Sandbox Code Playgroud)

JS:

$('#content').click(function(e) {
    var content = $(this);
    var editor = $('#editor');

    var position = content.offset();

    editor.css('left', position.left);
    editor.css('top', position.top);
    editor.val(content.text());
    editor.show();

    var mousePosition = { x: e.offsetX, y: e.offsetY };

    // here I want to set the #editor caret position at the same place,
    // where user clicks the #content …
Run Code Online (Sandbox Code Playgroud)

html javascript textarea

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

在jQuery ajax json响应中解析iso日期

我在json格式的jQuery-ajax响应中解析日期时遇到问题.

我的客户端代码:

$.ajax({
    url: '/index',
    dataType: 'json',
    success: function (data) {
        console.log(data.date);
    }
});
Run Code Online (Sandbox Code Playgroud)

服务器正在发送json:

{
    "name": "john",
    "date": "2013-07-01T00:00:00",
}
Run Code Online (Sandbox Code Playgroud)

运行我的客户端代码后,我在控制台中收到一个字符串内容:

2013-07-01T00:00:00
Run Code Online (Sandbox Code Playgroud)

这不是日期类型.我认为它将由解析器完成.在json解析期间,如何自动解析日期,我该怎么办?

克里斯

javascript ajax jquery json

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