小编jha*_*amm的帖子

如何为Jasmine/Angular创建一个帮助器来组合多个beforeEach

我一直在我的spec文件中重复一些注入模板然后编译它的代码.我将这段代码提取到一个辅助函数中,以保持干燥.我认为问题在于试图将其置于beforeEach辅助函数中.这是我试图抽象成函数的代码片段:

  beforeEach(module('app/views/header.html'));

  beforeEach(inject(function($templateCache, $compile, $rootScope) {
    template = $templateCache.get('app/views/header.html');
    $templateCache.put('views/header.html', template);
    var directive = angular.element('<clinical-header></clinical-header>');
    element = $compile(directive)($rootScope);
    $rootScope.$digest();
  }));
Run Code Online (Sandbox Code Playgroud)

这是我创建的辅助函数:

var setupTemplate = function(templateName, element) {
  beforeEach(module('app/views/' + templateName));
  beforeEach(inject(function($templateCache, $compile, $rootScope) {
    var template = $templateCache.get('app/views/' + templateName);
    $templateCache.put('views/' + templateName, template);
    var directive = angular.element(element);
    element = $compile(directive)($rootScope);
    $rootScope.$digest();
  }));
Run Code Online (Sandbox Code Playgroud)

现在这是对辅助函数的调用:

setupTemplate('header.html', '<clinical-header></clinical-header>');
Run Code Online (Sandbox Code Playgroud)

在我的辅助函数结束时,一切看起来都不错,但是当我跳到我的it块时,一切都是未定义的.我可以提取多个beforeEach?这样做的正确方法是什么?另外,放置茉莉花辅助功能的适当位置在哪里?如何完成?

javascript jasmine angularjs karma-runner

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

当我返回`false`时,为什么`_. map`返回undefined?

我有一个函数,用于映射数组underscore.这是功能:

var services = _.map(userCopy.get('services'), function (service) {
    if (service.service_selected === true) {
        return service;
    }
});
Run Code Online (Sandbox Code Playgroud)

我的问题是,当conditional === false我,然后我被undefined投入services.当然我可以删除undefined,但这很难看,我只是想map正确使用.我该如何解决?

javascript arrays underscore.js

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

如何让onClick连续工作 - reactjs

我试图点击甚至与表格一起工作reactjs.我的第一次尝试是使整行可以点击.这是我的代码:

var UserList = React.createClass({
  getInitialState: function() {
    return getUsers();
  },
  handleClick: function(e) {
    console.log("clicked");
  },
  render: function() {
    var users = this.state.users.map(function(user) {
      return (
        <tr onClick={this.handleClick}>
          <td>{user.name}</td>
          <td>{user.age}</td>
          <td></td>
        </tr>
      );
    });
    return(
      <div className="container">
        <table className="table table-striped">
          <thead>
            <tr>
              <th>Name</th>
              <th>Age</th>
              <th>Full Detail</th>
            </tr>
          </thead>
            <tbody>
              {users}
            </tbody>
        </table>
      </div>
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

这没用.然后我尝试在表格中添加一个按钮:

<button className="btn" onClick={this.handleClick}>Full Detail</button>
Run Code Online (Sandbox Code Playgroud)

这也行不通.我有其他onClick人在我的应用程序中工作,但我如何使用表格来完成这项工作?

reactjs

17
推荐指数
2
解决办法
3万
查看次数

我可以将templateUrl传递给指令 - AngularJS

有没有办法将a传递templateUrl给我的指令.我知道我可以使用翻译,但这似乎太多了.例如,我有一个widget指令,我想填写特定的HTML.有没有办法传递它像:

<div widget templateUrl="template1.html"></div>
<div widget templateUrl="template2.html"></div>
Run Code Online (Sandbox Code Playgroud)

javascript angularjs

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

你如何在Rails中路由[OPTIONS]?

我在Rails中创建一个REST服务.这是我的路线.

  resources :users
  match '/users', :controller => 'users', :action => 'options', :constraints => {:method => 'OPTIONS'}
Run Code Online (Sandbox Code Playgroud)

我能够[获取]我的用户.我正在尝试更新我的用户,但我收到错误消息:

ActionController::RoutingError (No route matches [OPTIONS] "/users/1"):
Run Code Online (Sandbox Code Playgroud)

当我在rake routes这里跑的是我给的路线:

    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
                 /users(.:format)          users#options {:method=>"OPTIONS"}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何修复我的路线,以便我可以进行任何类型的REST呼叫吗?谢谢.

rest activerecord routes ruby-on-rails-3

15
推荐指数
3
解决办法
9259
查看次数

为什么我的Handlebars没有编译方法?

我正在建立一个Backbone项目,Handlebars我遇到了一个Handlebars没有找到编译方法的问题.这是我的配置文件:

require.config({
  hbs: {
    templateExtension: '.hbs'
  },
  paths: {
    backbone: "libs/backbone/backbone",
    handlebars: 'libs/handlebars/handlebars.amd',
    hbs: 'libs/requirejs-hbs/hbs',
    jquery: 'libs/jquery/jquery',
    jqueryMockAjax: 'libs/jquery-mockjax/jquery.mockjax',
    text: 'libs/requirejs-text/text',
    templates: 'templates/',
    underscore: 'libs/underscore/underscore'
  },
  shim: {
    backbone: {
      deps: [
        'underscore',
        'jquery'
      ],
      exports: 'Backbone'
    },
    hbs: {
      deps: ['handlebars'],
      exports: 'hbs'
    },
    jqueryMockAjax: {
      deps: [ 'jquery' ],
      exports: '$.mockjax'
    },
    underscore: {
      exports: '_'
    }
  }
});

require(['app'], function(App) {
  'use strict';

  var app = new App();
  app.render();
});
Run Code Online (Sandbox Code Playgroud)

这是 …

javascript requirejs backbone.js handlebars.js

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

如何在JavaScript中的`Promise.all`中使用`Array#map`

我正在尝试Promise.all使用一系列项目创建一个.所以,如果我像这样创建它,它工作正常

Promise.all([
  Query.getStuff(items[0]),
  Query.getStuff(items[1])
]).then(result => console.log(result))
Run Code Online (Sandbox Code Playgroud)

如果我尝试创建Promise.all这样的,它不起作用

Promise.all([
    items.map(item => Query.getStuff(item))
]).then(result => console.log(result))
Run Code Online (Sandbox Code Playgroud)

then块在之前运行Query.getStuff(item).我错过了什么?

javascript promise

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

Rails 3中的link_to t()是什么?

今天早上我看了Railscast 328,我很难找到一个方法的文档.

<%= link_to t('.edit', :default => t("helpers.links.edit")),
                  edit_boy_scout_path(boy_scout), :class => 'btn btn-mini' %>
Run Code Online (Sandbox Code Playgroud)

我理解link_to方法,但我对t('edit ....)参数感到困惑,它在这个方法中调用了两次.解释甚至指向一些文档都会很棒.谢谢你的帮助

ruby ruby-on-rails-3

9
推荐指数
1
解决办法
7220
查看次数

如何在可选参数上查询postgres?

我正在设置REST服务,我正在使用它postgres作为数据存储.我想知道如何设置postgres查询以使用可选参数.即:

SELECT * from users 
where hair_color = $1 
and eye_color = $2
Run Code Online (Sandbox Code Playgroud)

$ 1和$ 2来自请求:[req.body.hair_color,req.body.eye_color]

如果用户没有通过该怎么办?eye_color在这种情况下,我想要所有的眼睛颜色.我假设我不必if/else在这里做一堆陈述.创建此查询的简洁方法是什么?

postgresql

9
推荐指数
1
解决办法
4671
查看次数

React Router如何进入React上下文?

我正在看redux-blog-example.有SignupRoute.js它看起来像这样:

@connect(state => ({
  auth: state.auth
}), {
  signup
})
export default class SignupRoute extends React.Component {
  static contextTypes = {
    router: React.PropTypes.object
  }

  handleSubmit = (email, password) => {
    const router = this.context.router;

    this.props.signup(email, password, router);
  }

  render() {
    return (
        <Signup
          auth={this.props}
          handleSubmit={this.handleSubmit}
        />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

如何router连接到context这个class

reactjs

9
推荐指数
1
解决办法
7109
查看次数