我想保留一个字符串列表,我只会检查它是否存在,例如:
corporatePlan = [
'canDeAuthorize',
'hasGmailSupport',
'canShareReports',
'canSummonKraken',
'etc'
]
Run Code Online (Sandbox Code Playgroud)
因此,当用户试图召唤海妖时,我会corporatePlan.indexof('canSummonKraken') != -1
看看他是否可以.
同事建议将它作为对象存储会更快:
"Corporate Plan" = {
'canDeAuthorize' : null,
'hasGmailSupport' : null,
'canShareReports' : null,
'canSummonKraken' : null,
'etc' : null
}
Run Code Online (Sandbox Code Playgroud)
并且只'canSummonKraken' in corporatePlan
需要检查计划是否包含该密钥.这在经典的CS意义上是有意义的,因为当然"包含"是地图上的恒定时间和数组上的线性.这会检查数组和对象是如何在JS中引入的吗?
在我们的特殊情况下,使用少于100个元素,速度并不重要.但是对于更大的阵列,访问哪种方式会更快?
所以,我正在尝试在rails项目的上下文中学习rspec BDD测试框架.我遇到的问题是,对于我的生活,我不能在rspec描述中正确加载我的灯具.
免责声明:是的,有比使用固定装置更好的东西.在尝试使用工厂女孩,摩卡,自动测试等相关工具之前,我一次尝试学习一件事(特别是rspec).因此,我试图让死者变得简单,如果笨重,固定装置工作.
无论如何,这是代码:
/test/fixtures/users.yml -
# password: "secret"
foo:
username: foo
email: foo@example.com
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
bar:
username: bar
email: bar@example.com
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
Run Code Online (Sandbox Code Playgroud)
/spec/controllers/pages_controller_spec.rb -
require 'spec/spec_helper'
describe PagesController do
integrate_views
fixtures :users
it "should render index template on index call when logged in" do
session[:user_id] = user(:foo).id
get 'index'
response.should render_template('index')
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行'rake spec'时我得到的是:
NoMethodError in 'PagesController should render index template on index call when logged in'
undefined method `user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0x2405a7c>
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/test_process.rb:511:in `method_missing' …
Run Code Online (Sandbox Code Playgroud) 所以 - 我有这个ajax请求,请参阅 - .大约6英尺高的金发女郎看起来像这样:
$.ajax({
url: 'http://example.com/makeThing',
dataType: 'html',
type: 'POST',
data: {
something:someotherthing
},
complete: function(request, status) {
console.log("headers=" + request.getAllResponseHeaders(););
}
});
Run Code Online (Sandbox Code Playgroud)
发生的事情是'/ makeThing'请求将302重定向返回到第二个url:'getThing/abc123'.我想要做的就是找出第二个网址是什么(以编程方式 - 它每次都会有所不同,所以只需查看firebug对我没有帮助).我已经尝试了回到'完整'回调的响应头,但这只是给了我第二次请求回来的内容.
约束: - 我无法控制正在运行的服务器 - 只有js. - 如果必须,可以切换框架(dojo?prototype?)
理想情况下,我会对/ makeThing执行某种仅限标头的请求,以通过获取初始302响应的标头来找出重定向URL的内容.
如果失败了(因为jquery自动跟随重定向并且似乎没有办法介入请求之间),我会检索最终响应并使用它以某种方式从...获取URL?请求对象,也许?
TLDR:发送ajax请求.框架自动跟随生成的302重定向.我如何确定它重定向到的位置?
编辑,澄清:最后的网址每次都会有所不同 - 调用'makeThing'会导致服务器创建以后在'getThing/abc123'中托管的内容
这很好用:
Func<string, string> func1 = s => s + "func";
ViewState["function"] = func1;
Run Code Online (Sandbox Code Playgroud)
但是,这不是:
Func<string, string> func1 = s => s + "func";
Func<string, string> func2 = s => func1(s);
ViewState["function"] = func2;
Run Code Online (Sandbox Code Playgroud)
它会抛出运行时序列化异常: Type 'MyProjectName._Default+<>c__DisplayClass3' in Assembly 'MyProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
现在,我可以解决这个问题,但是我想了解为什么会发生这种情况,以便将来除了在序列化之前编写函数之外我别无选择,我将有一个解决方案.
我在一些形式的Jasmine测试规范中有一个自定义匹配器:
this.addMatchers({
checkContains: function(elem){
var found = false;
$.each( this.actual, function( actualItem ){
// Check if these objects contain the same properties.
found = found || actualItem.thing == elem;
});
return found;
}
});
Run Code Online (Sandbox Code Playgroud)
当然,actualItem.thing == elem
实际上并没有比较对象内容 - 我必须在JavaScript中使用对象比较中的一个更复杂的解决方案.
不过,我不禁注意到,Jasmine已经有了一个很好的对象相等检查器:expect(x).toEqual(y).有没有办法在自定义匹配器中使用它?有没有在自定义匹配器中使用匹配器的一般方法?
背景:我有一个部署到heroku的项目.heroku应用程序与github连接,因此我可以按下heroku的web api中的"部署"按钮,手动将github上的分支部署到heroku.
我正在尝试做的是建立一个松弛的机器人,让我通过一个松弛的命令来实现这一目标.理想情况下,最好是在Heroku上的一些功能平台的API一样.deploy('my_app', 'some_branch_on_github)
,但我似乎无法找到它.
平台api的构建功能非常接近.该函数允许您提供heroku随后将部署的tarball的公共URL.但是,我的github repo不公开,所以这不起作用.私人回购应该不是问题,因为heroku已经连接到我的github仓库.
TLDR:我如何以编程方式告诉Heroku从它连接的私有github部署我的应用程序?
我正在尝试构建一个html文件来监视远程站点(尤其是github.com)上的某些内容。我希望能够将其保存到该平面文件中,从而将请求直接从JS发送到github的API。我的思考过程如下:
Not allowed to load local resource: file:///Users/...
出于可理解的安全原因。因此,关于如何通过单个本地html文件成功验证此api的任何建议-可以解决上述问题,也可以完全解决吗?
我怎么能有像 ActiveRecordscope
这样的东西来修改它所作用的关系?
具体来说,我希望能够做到这一点:
Model.some_scope.fake_destroy_all
Run Code Online (Sandbox Code Playgroud)
fake_destroy_all
我正在尝试创建的功能在哪里。它几乎等同于.update_all(deleted: true)
.
一个弱的替代方案是:
def fake_destroy_all(relation = Model)
relation.update_all(deleted: true)
end
#...
Model.fake_destroy_all(Model.some_scope)
Run Code Online (Sandbox Code Playgroud)
但这并不理想。我想做的是:
scope :fake_destroy_all, update_all(deleted: true)
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
是否有可能做我所描述的事情?
示例代码:
var div = new HtmlGenericControl("div");
div.Controls.Add(new Literal() { ID = "litSomeLit" });
var lit = (Literal)div.FindControl("litSomeLit");
Assert.IsNotNull(lit);
Run Code Online (Sandbox Code Playgroud)
此代码未通过断言,因为lit为null.调试显示div.Controls肯定包含ID为"litSomeLit"的文字.我的问题是"为什么?" 并且"有没有办法获得对特定ID的控制,而不是一次手动一个元素递归搜索div.Controls []?"
我这样做的原因是我的实际应用并不那么简单 - 我正在编写的方法给出了一个复杂的控件,在许多可能的配置中有几个子控件.我需要访问几个层的特定控件(例如,具有ID"txtSpecificControl"的控件可能在StartingControl.Controls[0].Controls[2].Controls[1].Controls[3]
).通常我可以这样做FindControl("txtSpecificControl")
,但是当控件刚刚动态创建时(例如上面的示例代码中),这似乎不起作用.
javascript ×4
ajax ×2
c# ×2
ruby ×2
activerecord ×1
asp.net ×1
cross-domain ×1
delegates ×1
dynamic ×1
findcontrol ×1
fixtures ×1
github ×1
github-api ×1
heroku ×1
heroku-api ×1
jasmine ×1
jquery ×1
jsonp ×1
matcher ×1
performance ×1
redirect ×1
rspec ×1
testing ×1
unit-testing ×1
web-controls ×1