在几乎所有我通过javascript动态注入脚本的例子中,它以:
document.getElementsByTagName("head")[0].appendChild(theNewScriptTag)
Run Code Online (Sandbox Code Playgroud)
甚至yepnope.js在页面中的第一个脚本之前附加新脚本,例如:
var firstScript = doc.getElementsByTagName( "script" )[ 0 ];
firstScript.parentNode.insertBefore( theNewScriptTag, firstScript );
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么不将它附加到文件正文?
document.body.appendChild(theNewScriptTag);
Run Code Online (Sandbox Code Playgroud)
在我看来,DOM遍历涉及getElementsByTagName- 甚至整个"insertAfter = parent.insertBefore"技巧 - 浪费资源.
将脚本动态添加到最底层是否有害?
理想情况下使用 DI ( Ninject ) 来管理范围内的上下文/存储库,但实际上仅限于服务定位器模式,不要问。理想的例子:
// EF code-first context
kernel.Bind<MyDbContext>().ToSelf().InRequestScope().WithConstructorArgument("connectionString",...);
// EF code-first repositories
kernel.Bind<IRepository<SomeModel>>().To<EfRepository<SomeModel, MyDbContext>>().InRequestScope();
kernel.Bind<IRepository<AuditLog>>().To<EfRepository<AuditLog, MyDbContext>>().InRequestScope();
Run Code Online (Sandbox Code Playgroud)在我的服务调用中,我尝试将错误数据保存到数据库,因此存储库/上下文.SaveChanges()正确失败:
try {
var model = new SomeModel { Name = ..., Blah = ... }
repoSomeModels.Add(model);
repoSomeModels.Commit(); // bad data, throws exception
}
Run Code Online (Sandbox Code Playgroud)然后我想将这次失败尝试的记录记录到同一个数据库中:
catch(Exception ex) {
repoAuditLogs.Add(new AuditLog { Action = ..., Result = ex.Message });
repoAuditLogs.Commit();
throw ex; // rethrow the problem so we can properly bail
}
Run Code Online (Sandbox Code Playgroud)但是,当尝试保存第二次提交()中的更改时,repoAuditLogs.Commit()我再次遇到相同的异常。这是因为即使尝试失败,第一次尝试提交的“坏数据”仍然存在于 EF 上下文中,因此 EF 再次尝试提交坏数据,但不知道不应该这样做。 …
我试图将从richeditDocument生成的内存流转换为字节数组.代码如下:
Public Sub saveAsTemplate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ms As MemoryStream = New MemoryStream()
richEditControl1.SaveDocument(ms, DocumentFormat.Rtf)
GetStreamAsByteArray(ms)
MessageBox.Show("save")
End Sub
Private Function GetStreamAsByteArray(ByVal stream As MemoryStream) As Byte()
Dim streamLength As Integer = Convert.ToInt32(stream.Length)
Dim fileData As Byte() = New Byte(streamLength) {}
' Read the file into a byte array
stream.Read(fileData, 0, streamLength)
stream.Flush()
stream.Close()
Return fileData
End Function
Run Code Online (Sandbox Code Playgroud)
生成流时我可以获得流长度,但最终的咬合数组只包含0,使其无效.我怎样才能获得正确的字节数组?
基本上遵循代码示例逐字,但尝试使用fetch发出POST请求
fetch('YOUR URL HERE', { method: 'POST', 'body': content })
.then(function (res) {
console.log(res.text());
return res.text();
})
.then(function (plain) {
var output = { okay: true, raw: plain };
callback(null, output);
})
.catch(callback);
Run Code Online (Sandbox Code Playgroud)
结果在愤怒的红色框中:
我们无法通过您的测试.请再试一次.错误:错误:正文已用于:https:// YOURURLHERE
不确定这意味着什么?
在编写一个javascript函数来评估多变量条件时,我遇到了似乎是Javascript中的解析器错误.如果我忘了某件事,或者这是否合适,请告诉我.
在我的函数中,我将返回AND几个变量的结果,如下所示:
return
// a comment, for kicks
a1 && a2
&&
b1 && b2
&&
// another comment
c1 && c2
&&
d1 && d2
;
Run Code Online (Sandbox Code Playgroud)
但是,即使所有这些变量都具有显式值true,函数也会返回undefined而不是预期值true.
我已经尝试了几种返回此表达式的变体,我发现:
查看工作示例:http://jsfiddle.net/drzaus/38DgX/
任何人都可以解释为什么会这样吗?
背景:按位枚举对于"更易读"的比较和检查非常有用:即OpenFile(write | append).
我已经看到了几种在C#中声明按位枚举的方法,但是最近其中一种常见模式似乎不再返回唯一值,我想知道我是在宣告它是错还是有些东西已经改变了.我在谈论"DWORD"(十六进制?)样式(如下所示),在VS2012 RC中枚举时,值为1,2,3,4 ...而不是预期的逐位加倍.
其他人可以重现这个吗?我发布了用于验证的代码以及控制台输出; ComparisonsDword正如您可以通过"标记枚举,带有DWORD的显式值"的输出所看到的那样发生奇怪的行为.
没有标志,正常的枚举
/// <summary>
/// How to compare filter values; no underlying type declared, not flag
/// </summary>
public enum ComparisonsNotInt {
[Description("x")]
None
,
[Description("!=")]
NotEqual
,
[Description("=")]
Equal
,
[Description(">")]
GreaterThan
,
[Description("<")]
LessThan
,
/// <summary>
/// Combination of <see cref="Equal"/> and <see cref="LessThan"/>
/// </summary>
[Description("<=")]
LessThanOrEqual = (Equal | LessThan)
,
/// <summary>
/// Combination of <see cref="Equal"/> and <see cref="GreaterThan"/>
/// </summary>
[Description(">=")]
GreaterThanOrEqual …Run Code Online (Sandbox Code Playgroud) javascript ×2
bytearray ×1
c# ×1
debugging ×1
dom ×1
enums ×1
fetch-api ×1
flags ×1
optimization ×1
parsing ×1
silverlight ×1
syntax ×1
vb.net ×1
zapier ×1