我正在查看一些代码片段,我发现多个元素在一个节点列表上调用一个函数,并将forEach应用于一个空数组.
例如,我有类似的东西:
[].forEach.call( document.querySelectorAll('a'), function(el) {
// whatever with the current node
});
Run Code Online (Sandbox Code Playgroud)
但我无法理解它是如何工作的.任何人都可以解释我在forEach前面的空数组的行为以及它是如何call工作的?
我在我的主模板中定义了一个全局变量,我用它来存储来自后端的信息位,例如环境上下文路径.我无法在服务中移动该变量.
当我运行单元测试时,如何将该变量公开给Karma?
我的范围内有一个函数可以在用户单击按钮时检索我的服务状态,或者触发某个事件并自动调用此函数.
这是我的功能,在我使用的控制器中定义:
$scope.getStatus = function() {
$http({method: 'GET', url: config.entrypoint + config.api + '/outbound/service/' + $scope.serviceId})
.success(function(data) {
$scope.errorMessage = '';
$scope.serviceAllGood = data;
})
.error(function() {
$scope.serviceAllGood = '';
$scope.errorMessage = 'We are experiencing problems retrieving your service status.';
});
}
Run Code Online (Sandbox Code Playgroud)
单元测试如下:
describe('SendServiceCtrl', function(){
var scope, ctrl, $httpBackend, configuration;
beforeEach(function () {
module('papi', 'ngUpload');
});
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, config) {
configuration = config;
$httpBackend = _$httpBackend_;
$httpBackend.expectGET(configuration.entrypoint + configuration.api + "/user/outboundstatus/").respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"allowed":false}});
scope = $rootScope.$new();
ctrl = $controller('SendServiceCtrl', {$scope: …Run Code Online (Sandbox Code Playgroud) 我试图让Redux Form第一次工作,我收到以下错误:
删除了不变违反withRef.要访问包装的实例,请在连接的组件上使用ref.
我究竟做错了什么?一旦我写了(从示例中复制/粘贴)商店,就会抛出错误.
这是代码.
import React from "react";
import ReactDOM from "react-dom";
import { createStore, combineReducers } from 'redux'
import { reducer as formReducer } from 'redux-form'
const rootReducer = combineReducers({
form: formReducer
})
const store = createStore(rootReducer);
function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Run Code Online (Sandbox Code Playgroud)
我还创建了一个代码沙箱来显示问题:https://codesandbox.io/s/07xzolv60
我想在Yeoman/Grunt项目中使用TypeScript.要编译TypeScript我使用一个名为grunt-ts的grunt插件,.ts文件的编译工作正常,但实时重新加载不起作用:当我运行时grunt server我正确得到这个:
Running "ts:dev" (ts) task
Compiling.
Success: 3.37s for 2 typescript files
Watching all Typescript files under : /home/mimo/webroot/tsyong/app/scripts
Run Code Online (Sandbox Code Playgroud)
但是然后没有加载liveReload任务.这就是我如何配置关于grunt-ts的Gruntfile.js.
grunt.initConfig({
...
ts: {
options: { // use to override the default options, http://gruntjs.com/configuring-tasks#options
target: 'es3', // es3 (default) / or es5
module: 'commonjs', // amd , commonjs (default)
sourcemap: true, // true (default) | false
declaration: false, // true | false (default)
nolib: false, // true | false (default)
comments: false // true | false (default) …Run Code Online (Sandbox Code Playgroud) 在检查一些代码时,我发现了这个新的声明:-webkit-padding-start但是我无法理解与现有padding-left属性有什么区别.我已经阅读了有关Mozilla开发人员的页面并创建了一个小提琴,但我仍然不确定.
我试图避免在工厂中向服务器发出多个ajax请求.我已经添加了一个小的缓存服务,但这还不足以满足我的目标:在服务器响应之前可以多次调用此工厂,从而导致向服务器生成多个请求.
为了避免这种情况,我添加了第二个promise对象,如果已经执行了AJAX请求并且对象尚未在缓存中,那么它应该等待第二个promise被解析,但看起来我错过了一些东西.
这是我的代码:
myApp.factory('User', ['Restangular', '$q',
function (Restangular, $q) {
var userCache, alreadyRun = false;
return {
getUser: function () {
var deferred = $q.defer(), firstRun= $q.defer();
if (!userCache && !alreadyRun) {
alreadyRun = true;
Restangular.all('user').getList().then(function (user) {
console.log('getting user live ');
userCache = user[0].email;
firstRun.resolve(user[0].email);
});
} else if (!userCache && alreadyRun) {
console.log('waiting for the first promise to be resolved ');
firstRun.then(function(user) {
console.log('resolving the promise');
deferred.resolve(userCache);
});
} else {
console.log('resolving the promise from the cache');
deferred.resolve(userCache) …Run Code Online (Sandbox Code Playgroud) 我正在使用Restangular构建一个应用程序.我会在一个地方实现处理错误,如401,403,500等在响应拦截器中.
这就是我写的:
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
switch(response.meta.code) {
case 200:
// Handle 200
break;
case 401:
// Redirect to login
break;
/* ... */
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
实际发生的是200正确看到但是这个方法根本没有被401命中.我从服务器端获得的JSON格式如下,以便成功响应:
{"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"response":"value"}}
Run Code Online (Sandbox Code Playgroud)
和
{"meta":{"apiVersion":"0.1","code":401,"errors":[{"code":401,"message":"Unauthorized"}]},"response":null}
Run Code Online (Sandbox Code Playgroud)
为了错误.响应头也包含Status Code:401 Unauthorized.我究竟做错了什么?
我正在学习Haskell并遵循http://learnyouahaskell.com/starting-out上的指南.我正处于显示的位置:
ghci> let nouns = ["hobo","frog","pope"]
ghci> let adjectives = ["lazy","grouchy","scheming"]
ghci> [adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
["lazy hobo","lazy frog","lazy pope","grouchy hobo","grouchy frog",
"grouchy pope","scheming hobo","scheming frog","scheming pope"]
Run Code Online (Sandbox Code Playgroud)
我想要实现的,它是类似的,但结合两个字符串中包含的字母,因为字符串基本上是Haskell中的char列表,这是我尝试的:
[x ++ ' ' ++ y | x <- "ab", y <- "cd"]
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨:
Prelude> [y ++ ' ' ++ y | x <- "abd", y <- "bcd"]
<interactive>:50:2:
Couldn't match expected type ‘[a]’ with actual type ‘Char’
Relevant bindings include …Run Code Online (Sandbox Code Playgroud) 我刚刚开始通过示例学习purescript ,但是我在第2章的末尾使用了grunt项目模板.我按照说明操作,但是当我运行grunt命令时,我收到此错误:
/Users/chad/playground/purescript/tests/tmp/node_modules/Control.Monad.Eff.Random/index.js:8
return Math.random();
^
TypeError: undefined is not a function
at Object.random (/Users/chad/playground/purescript/tests/tmp/node_modules/Control.Monad.Eff.Random/index.js:8:17)
at __do (/Users/chad/playground/purescript/tests/tmp/node_modules/Test.QuickCheck/index.js:177:52)
at Object.__do (/Users/chad/playground/purescript/tests/tmp/node_modules/Main/index.js:19:201)
at Object.<anonymous> (/Users/chad/playground/purescript/tests/tmp/index.js:1:79)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
Warning: -> error 1 /Users/chad/playground/purescript/tests/tmp/index.js (125ms) Use --force to continue.
Aborted due to warnings.
Run Code Online (Sandbox Code Playgroud)
我通过深入研究代码发现,模块Math被导入为/output/Control.Monad.Eff.Random/index.jsas
var Math = require("Math");
function random() {
return Math.random();
}
;
Run Code Online (Sandbox Code Playgroud)
如果我删除导入,让Math是原生的并且index.js简单地运行main …
我是一个Rails新手,因此我遵循入门指南,可在此处获取:http://edgeguides.rubyonrails.org/getting_started.html此处:http://guides.rubyonrails.org/getting_started.html但我可以没有达到5.6/5.7的要求.
这是我的控制器:
class PostsController < ApplicationController
def new
end
def show
@post = Post.find(params[:id])
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的形式:
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %> <br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这是routes.rb
Blog::Application.routes.draw do …Run Code Online (Sandbox Code Playgroud) angularjs ×4
jasmine ×2
javascript ×2
karma-runner ×2
arrays ×1
char ×1
css3 ×1
ecmascript-5 ×1
ffi ×1
foreach ×1
gruntjs ×1
haskell ×1
html ×1
html5 ×1
httpbackend ×1
livereload ×1
mozilla ×1
node.js ×1
nodelist ×1
promise ×1
purescript ×1
q ×1
reactjs ×1
redux ×1
redux-form ×1
restangular ×1
ruby ×1
string ×1
typescript ×1
unit-testing ×1
webkit ×1
yeoman ×1