小编Cri*_* E.的帖子

将javascript函数作为data-*属性传递并执行

在定义valuefor onClick属性时,我们知道如下语法:

<button type="submit" onclick="alert('hi');"></button>
<button type="submit" onclick="doWork"></button> <!-- This one doesn't work -->
<button type="submit" onclick="doWork()"></button>
<button type="submit" onclick="doWork('Mike', 2)"></button>
Run Code Online (Sandbox Code Playgroud)

我感兴趣的是定义一个自定义data-attribute并执行如下值:

<button type="submit" data-callback="alert('hi');"      class="marker"></button>
<button type="submit" data-callback="doWork"            class="marker"></button>
<button type="submit" data-callback="doWork()"          class="marker"></button>
<button type="submit" data-callback="doWork('Mike', 2)" class="marker"></button>

<script type="text/javascript">
    jQuery("body").on("click","button.marker", function(e) {
        var callback = jQuery(e.currentTarget).data("callback");

        // Now I need to execute the callback no matter of the format
        // 1. Execute as function's body
        // 2. Or by function 'name'
        // 3. …
Run Code Online (Sandbox Code Playgroud)

javascript jquery custom-data-attribute

24
推荐指数
4
解决办法
2万
查看次数

Next.js - 是否可以调试 getServerSideProps?

您好,根据文档getServerSideProps是一个仅在服务器端执行的函数。

export async function getServerSideProps(context) {
  console.log("hello world");
  debugger;
  return {
    props: {
      age:55
    }, // will be passed to the page component as props
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,简单地删除调试器关键字是行不通的。

我努力了:

  • 在端口 9229 上附加 node.js 调试器
  • 跑步node --inspect-brk ./node_modules/next/dist/bin/next
  • 跑步cross-env NODE_OPTIONS='--inspect' next dev

console.log()debugger关键字似乎都没有任何效果。

然而,我来自 getServerSideProps 的道具被正确传递,这证明该函数被调用。

是否可以单步执行 getServerSideProps 或至少让 console.log 工作?谢谢!

聚苯乙烯

getServerSideProps 外部的代码行是可调试的并且可以按预期工作。

next.js

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

Azure Pipelines - 有没有办法查看文件夹结构?

我正在努力描绘 azure 管道的文件夹结构。我知道有一些隐式目录,例如:

  • $(System.DefaultWorkingDirectory)
  • $(Build.ArtifactStagingDirectory)

这两个文件夹都是池中可用的特定构建代理上的文件夹。

有没有办法查看文件夹结构并更好地了解事物的布局?

azure-devops azure-pipelines

13
推荐指数
4
解决办法
9688
查看次数

Typescript:无法在映射类型上声明其他属性

我在使用 TypeScript 输入对象时遇到问题

我声明了应用程序的某些文件使用的类型:

type Category = "cat1"|"cat2"|"cat3"|"cat4"
Run Code Online (Sandbox Code Playgroud)

我用来Category输入一个对象并且一切正常使用:

const obj: {
  [key in Category]: string
} = {---}
Run Code Online (Sandbox Code Playgroud)

但现在,我想添加 2 个不是Category对象值的键。我认为通过输入这样的对象会很容易:

const obj: {
  customKey1: string
  customKey2: string
  [key in Category]: string
} = {---}
Run Code Online (Sandbox Code Playgroud)

但 TS 没有按预期工作,而是发送了 3 个错误:TS2464、TS1170 和 TS2693

A computed property name must be of type 'string', 'number', 'symbol' or 'any'. ts(2464)
'Category' only refers to a type, but is using as a value here. ts(2693)
A computed property name in …
Run Code Online (Sandbox Code Playgroud)

typescript

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

React Router 4 Regex路径-匹配找不到参数

我正在尝试为反应路由器4匹配正则表达式模式,但不幸的this.props.match.params.id是,它无法正确解析路径,并显示为未定义。

我希望能够访问的路径示例:

  1. /全球定位系统
  2. / gps /空气
  3. / gps / a0b6dc45-11a1-42c5-afdb-a62822d109dc
  4. / gps / air / a0b6dc45-11a1-42c5-afdb-a62822d109dc

我的组件:

import React from "react";
import PropTypes from "prop-types";
import { withRouter, } from "react-router";
import { Switch, Route } from "react-router-dom";

class Foo extends React.Component {
    render(){
        console.log(this.props.match);
        return <div>Match: {this.props.match.params.id}</div>
    }
} 

const industryRegex = "(air|motor|ship)";
const guidRegex = "([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})?"; // Note the questionmark ? at the end

const pathId =            `/gps/:id${guidRegex}`;
const pathIndustry =      `/gps/:industryType${industryRegex}`;
const pathIndustryAndId = `/gps/:industryType${industryRegex}/:id${guidRegex}`; …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router react-router-v4

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

Typescript 映射类型 - 无法添加其他属性

假设我有这样的类型定义:

name必须具有或fullname已定义财产的人

type Person = {
  [k in "name" | "fullname"]: string;
}; 
Run Code Online (Sandbox Code Playgroud)

假设我想再添加一个必需的属性age,直观上我会写这样的内容:

type Person = {
  [k in "name" | "fullname"]: string;
  age: number; // This errors
}; 
Run Code Online (Sandbox Code Playgroud)

然而,这种语法不起作用,唯一的方法是使用交集运算符,&如下所示:

type Person = {
  [k in "name" | "fullname"]: string;
} & { age: number }; 
Run Code Online (Sandbox Code Playgroud)

我是否遗漏了某些内容,或者交集是在映射类型之上定义此附加属性的唯一方法?

游乐场在这里

typescript

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

React Redux - 为什么在构造函数之前调用mapStateToProps?

两个问题:

  1. 为什么mapStateToProps在构造函数之前调用?
  2. 作为1的副作用

    constructor (props) { base(props) // props already have values from "mapStateToTprops" }

为什么要自动完成?

  1. 不是每次mapStateToProps调用ComponentWillReceiveProps(这是第一次加载时的情况)请参阅此链接在此处输入链接描述

更新1

如果我想写一个像这样的条件:

if (props.isAuthenticated) { browserHistory.push("/admin/dashboard") }

哪种方法最适合挂钩.请记住,我想在每次状态更改时强制执行此条件(因为根据leo的回答,ComponentWillReceiveProps不可靠)?

reactjs redux react-redux

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

Ajax.BeginForm(),OnSuccess - 获取事件目标

我无法获得触发OnSuccess()方法的元素的目标Ajax.BeginForm().所以这是代码片段:

@using (Ajax.BeginForm("InsertModel", "Home", new AjaxOptions
{
    HttpMethod = "POST",
    OnSuccess = "doWork(this,'SomeCustomText')"
}))
{
    <div id="container">
       <--Some HTML--!>
      <input type="submit" value="OK"/>
    </div>
}

<script>
function doWork(e,customText)
{
   alert(customText);  //It shows 'SomeCustomText', so its good.

   alert(e);  //[Object] object
   alert(e.prop("tagName"));  //Object #<Object> has no method 'prop' 
   alert(e.attr("tagName"));  //Object #<Object> has no method 'attr' 
   alert(jQuery(e).html());  //undefined
   alert(jQuery(e).prop("tagName"));  //undefined
   alert(e.target);  //undefined
   alert(jQuery(e).target);  //undefined
 }
<script/>
Run Code Online (Sandbox Code Playgroud)

问题:

如何获得目标?!谢谢

更新1

jQuery版本应该如下所示:

jQuery.ajax({
    url:"/Home/InsertModel",
    data:"some post data",
    type:"POST",
    success:function(data){ 
          doWork(this,data); // so …
Run Code Online (Sandbox Code Playgroud)

c# ajax.beginform asp.net-mvc-3

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

Autofac.如何获得调用者类类型?

假设我们有两个具有相同构造函数的类Injectable依赖:

public class FirstClass
{
    public FirstClass(ISomeDependency someDependency)
    { }
}

public class SecondClass
{
    public SecondClass(ISomeDependency someDependency)
    { }
}
Run Code Online (Sandbox Code Playgroud)

现在我们注册ISomeDependency:

builder.Register(x =>
        {
            string key = GetKeyFromCurrentHttpRequest();
            // if "Caller" is "FirstClass" return new Dependency(key);
            // else return new Dependency("defaultKey");
        }).As<ISomeDependency>();
Run Code Online (Sandbox Code Playgroud)

注意:这是一个简化的用例.真实场景要复杂得多.

1.如何获得试图解决ISomeDependency的"Caller"类型?

2.有没有更好的方法来设计这种情况?

dependency-injection ioc-container inversion-of-control autofac

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

使用IoC在Controller中注入HttpContextBase的目的是什么?

我已经看到许多使用IoC容器的代码示例,注册如下:

// Autofac
builder.Register(c => new HttpContextWrapper(HttpContext.Current))
            .As<HttpContextBase>()
            .InstancePerRequest();

// Again Autofac 
builder.RegisterModule(new AutofacWebTypesModule());
Run Code Online (Sandbox Code Playgroud)

(请参阅此处的AutofacWebTypesModule的src )

// Castle Windsor
container.Register(Component.For<HttpContextBase()
                  .LifeStyle.PerWebRequest
                  .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Run Code Online (Sandbox Code Playgroud)

与使用构造函数注入的控制器一起:

public class HomeController : Controller
{
    private readonly HttpContextBase _httpContext;

    public HomeController(HttpContextBase httpContext)
    {
        _httpContext = httpContext;
    }
    //....
}
Run Code Online (Sandbox Code Playgroud)

问题1:

你能解释一下包装HttpContextBase,HttpRequestBase等的原因吗?

问题2:

注入的HttpContextBaseHttpContext(Controller的属性)与System.Web.HttpContext.Current之间的区别是什么?

更新

问题3:

在代码中使用哪个HttpContext,Injected One或者它也可以通过HttpContextSystem.Web.HttpContext.Current调用它?如果两种方式都有问题吗?

asp.net-mvc dependency-injection castle-windsor inversion-of-control autofac

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