在javascript中,我经常想要访问可能不存在的对象的属性.
例如:
var foo = someObject.myProperty
但是,如果未定义someObject,则会抛出错误.访问可能为空的对象的属性的常规方法是什么,如果不存在则返回false或null?
在Ruby中,我可以做到someObject.try(:myProperty).是否有JS等价物?
使用Swift,如何从中获取输入UITextField?
在我的Main.storyboard中,我从预制组件中选择了一个"文本字段"并将其拖到我的故事板上.
现在我如何在ViewController.swift中访问它的值?
我目前在做什么:
我按住Ctrl键并将文本字段拖到我的ViewController.swift文件中.然后在弹出框中我选择"outlet"并将其命名为"myInput".
然后在另一个函数中,我通过使用来访问它的值self.myInput.text.
这是访问其输入的唯一方法吗?我的方法遵循Swift惯例吗?
我在Ajax调用中发送一些数据.其中一个值是布尔值设置为FALSE.它始终在Ajax调用的PHP脚本中被评估为TRUE.有任何想法吗?
$.ajax({
type: "POST",
data: {photo_id: photo_id,
vote: 1,
undo_vote: false}, // This is the important boolean!
url: "../../build/ajaxes/vote.php",
success: function(data){
console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)
在vote.php中,在上面的Ajax中调用的脚本,我检查了布尔值:
if ($_POST['undo_vote'] == true) {
Photo::undo_vote($_POST['photo_id']);
} else {
Photo::vote($_POST['photo_id'], $_POST['vote']);
}
Run Code Online (Sandbox Code Playgroud)
但$_POST['undo_vote'] == true条件总是得到满足.
如何设置URL参数History.pushState()以避免浏览器刷新?如果有一个不简单的JS解决方案,是否已经有一个流行的库或jQuery的内置函数?
这是一个相关的SO问题,其中接受的答案根据注释和我的测试实际上不起作用(它删除查询字符串而不是更新值):history.pushState()更改查询值
为了清楚起见,我指的是查询字符串中的URL参数:
http://google.com/page?name=don所以我们可以更改don为tim而不会导致重新加载.
这是我找到的一种可能的解决方案.但是我很担心使用只有2个粉丝的JS库:P
我想有条件地将类"hidden"添加到Rails链接标记,具体取决于"accepted == true".
如果我不使用导轨,link_to我可以做<a href="#" class="foo bar <%= "hidden" if accepted == true %>" >.我怎么能在里面完成这个link_to?
<%= link_to "Accept Friend Request",
"#",
class: "btn btn-success btn-sm btn-block requestSent ???hidden???",
disabled: true %>
Run Code Online (Sandbox Code Playgroud) 我正在使用Rspec进行测试.但是,它使用我的开发数据库而不是我的测试数据库.怎么会发生这种情况?
我只是使用rspec来运行我的测试: don:my_project_root $ rspec
看起来其他问题的常见错误只与Rails 3相关或者已经过时了(使用不再与rspec一起使用的命令).
下面是我的spec_helper.rb.
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require "capybara/rspec"
require 'database_cleaner'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If …Run Code Online (Sandbox Code Playgroud) 如何从文本输入更新redux的状态?
我正在尝试用文本输入做一个非常简单的"Hello World".当有人输入文本输入时,它应该更新我的商店的"searchTerm"值.
我无法弄清楚这些事情:1.我怎样才能将输入的值传递给它的"onChange"处理程序?2."搜索"操作似乎被正确调用,但我的reducer函数从未使用过(没有console.log).
SearchForm.js(组件)
import React, {Component, PropTypes} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {search} from 'redux/modules/search-term';
@connect(
null,
dispatch => bindActionCreators({ search }, dispatch)
)
export default class SearchForm extends Component {
static propTypes = {
search: PropTypes.func.isRequired,
}
render() {
return (
<input type="text" placeholder="Search" onChange={search} />
);
}
}
Run Code Online (Sandbox Code Playgroud)
search-term.js(动作和减速器)
const SEARCH = 'redux-example/repo-filter/SEARCH';
const initialState = {
searchTerm: null
};
export default function reducer(state = initialState, action = …Run Code Online (Sandbox Code Playgroud) 我正在使用Jest来测试我的React应用程序.
最近,我将DeckGL添加到了我的应用程序中.我的测试因此错误而失败:
Test suite failed to run
/my_project/node_modules/deck.gl/src/react/index.js:21
export {default as DeckGL} from './deckgl';
^^^^^^
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)
at Object.<anonymous> (node_modules/deck.gl/dist/react/deckgl.js:9:14)
at Object.<anonymous> (node_modules/deck.gl/dist/react/index.js:7:15)
Run Code Online (Sandbox Code Playgroud)
这似乎是Jest在运行它的测试之前转换节点模块的问题.
这是我的.babelrc:
{
"presets": ["react", "es2015", "stage-1"]
}
Run Code Online (Sandbox Code Playgroud)
这是我的开玩笑设置:
"jest": {
"testURL": "http://localhost",
"setupFiles": [
"./test/jestsetup.js"
],
"snapshotSerializers": [
"<rootDir>/node_modules/enzyme-to-json/serializer"
],
"moduleDirectories": [
"node_modules",
"/src"
],
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/test/EmptyModule.js"
}
},
Run Code Online (Sandbox Code Playgroud)
我似乎有正确的事情需要改造export {default as DeckGL }.所以任何想法都会出错?
什么时候CSS'font-family'的值有引号?
我已经看到了这两个font-family: arial和font-family: "arial".
作为一个具体的例子,我这样声明一种字体:
@font-face {
font-family: 'entypo';
font-style: normal;
font-weight: 400;
src: url('/css/fonts/entypo.woff') format('woff');
}
Run Code Online (Sandbox Code Playgroud)
有一个元素使用这个font-family的正确CSS是什么?