标签: coffeescript

什么是用户代理字符串(github api)

所以,我正在尝试向github发出HTTP请求,以返回特定github存储库中所有问题的列表.我在下面的代码中使用了coffeescript,但对于任何JS开发人员来说都应该是相当自我解释的.我的困惑是,如果我在浏览器中输入" https://api.github.com/repos/username/repo-name/issues ",我可以检索到我正在寻找的所有信息.当我尝试通过我的应用程序使用请求节点库发出请求时,我收到一条错误消息"Missing or invalid User User string" .如果您知道如何正确构建URL以实际从github API检索信息,请告诉我.

githubUrl = "https://api.github.com/repos/#{username}/#{repoName}/issues?state=open"

    request githubUrl, (error, response, body) ->

        console.log body
Run Code Online (Sandbox Code Playgroud)

javascript api github node.js coffeescript

0
推荐指数
1
解决办法
1384
查看次数

CoffeeScript实例方法

我有一个像这样定义的CoffeeScipt类

class Foo
  a: 1
  b: 2
  main: ->
   if a == 1
    log(1)

  log: (num) ->
    console.log(num)
f = new Foo
f.main()
Run Code Online (Sandbox Code Playgroud)

它一直错误地说没有定义日志.我试过让它@log:也不起作用.我尝试制作->主要的a =>并且也没有用.如何从类本身调用实例方法?

instance-methods coffeescript

0
推荐指数
1
解决办法
882
查看次数

来自Coffeescript的臃肿的JS想要归还一切

我在这里有这个Coffeescript:

brew = (args...) =>
  for e in args
    alert e
    null

brew('fo', 're', 'eo');
Run Code Online (Sandbox Code Playgroud)

我希望我不需要把null放在那里让它工作,但唉,编译成这个:

brew = function() {
  var args, e, _i, _len, _results;
  args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  _results = [];
  for (_i = 0, _len = args.length; _i < _len; _i++) {
    e = args[_i];
    alert(e);
    _results.push(null);
  }
  return _results;
};

brew('fo', 're', 'eo');
Run Code Online (Sandbox Code Playgroud)

但现在我有3条不必要的线:

 _results = [];
 _results.push(null);
 return _results;
Run Code Online (Sandbox Code Playgroud)

有小费吗?

coffeescript

0
推荐指数
1
解决办法
90
查看次数

使用coffescript删除数组的重复元素

我有一个元素重复的数组:

data = ["Ruby on rails", "Ruby on rails", "Jquery", "Coffescript", "Javascript"]
Run Code Online (Sandbox Code Playgroud)

我尝试删除数组的重复元素

indexes = []
uniques = []
i = 0
while i < data.length
 if indexes[data[i].text] is "undefined"
  indexes[data[i].text] = "defined"
  uniques.push
i++
console.log data
Run Code Online (Sandbox Code Playgroud)

但是我得到了与元素重复相同的结果.

我怎么修好?

谢谢!

javascript jquery coffeescript

0
推荐指数
1
解决办法
631
查看次数

读一个有"'"的json字符串(格式错误?)

我需要使用返回JSON的服务.我对该服务(第三方)没有任何影响.

如果我做

JSON.parse (data)
Run Code Online (Sandbox Code Playgroud)

我得到了

SyntaxError: Unexpected token
Run Code Online (Sandbox Code Playgroud)

我知道该服务有效,因为错误发生取决于输入参数.换句话说,有时肯定会有效!HTTP响应代码是200,因此它不是某种访问错误,并且它是可重复的.

我可以假设他们正在返回格式错误的JSON吗?将数据作为文本文件写入磁盘并按如下方式读取:

fs = require('fs')

fs.readFile 'output.json', '', (err, data) ->
  if err?
    console.log err
  json = JSON.parse(data)
  console.log json
Run Code Online (Sandbox Code Playgroud)

回报

undefined:1
 De L\'Embustier
      ^
SyntaxError: Unexpected token '
    at Object.parse (native)
Run Code Online (Sandbox Code Playgroud)

这有点奇怪,因为好像字符串被正确转义,但它仍然没有被正确读取.

文件是300 + k; 还没有看到如何附上它.

编辑:来自jsonlint.com的回复

Parse error on line 1297:
...         "Address": "36 Rue De L\'Embust
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
Run Code Online (Sandbox Code Playgroud)

EDIT2:这是整个文件:http: //pastebin.com/ACUfvPCx

javascript json node.js coffeescript

0
推荐指数
1
解决办法
3355
查看次数

是否可以解析咖啡脚本资产文件中的Ruby实例变量?

我试图将实例变量从我的rails应用程序传递到关联的coffeescript文件,但它目前似乎没有解析.我错过了什么?

locations.js.coffee.erb

$.ajax
  url: "/map_groups/<%= @id %>.json"
  type: "get"
  dataType: "json"
  async: false
  success: (response) ->
    exports.response = response
Run Code Online (Sandbox Code Playgroud)

locations_controller.rb

def index
  @id = (params[:id]) ? params[:id] : 1
  @locations = Location.all
end
Run Code Online (Sandbox Code Playgroud)

但这是控制台中出现的错误:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/map_groups/.json
Run Code Online (Sandbox Code Playgroud)

有什么办法可以让实例变量解析吗?

注意:

我知道变量存在,因为它被传递给视图.

编辑:我想做什么

我的大多数数据都是通过JSON发送的,我创建了一个自定义路由,以便让coffeescript知道要提取的json数据:

get "/locations/map-group/:id", controller: :locations, action: :index, as: :map_group
Run Code Online (Sandbox Code Playgroud)

如果您返回我的控制器 - 您将看到如果用户访问普通旧/locationsID,则默认为1.否则,id是URL中指定的内容.coffeescript文件需要通过AJAX调用提取与该ID相关的数据.我如何告诉咖啡因该ID是什么?

javascript ruby-on-rails coffeescript

0
推荐指数
1
解决办法
1838
查看次数

html/javascript:防止表单提交时清除<input>值

我有一个只包含一个<input>元素的表单:

    <form>
            <input
                    type="text"
                    name="webpage-activity-url"
                    placeholder="URL"
                    value="[value inserted by templating engine]"
            />
            <button type="submit" class="button button-secondary" data-action="urlSubmit">
                    Submit
            </button>
    </form>
Run Code Online (Sandbox Code Playgroud)

我还有一些javascript(以coffeescript的形式),在提交表单时调用:

    onURLSubmit: (e)->
            e.preventDefault()
            # console.log('urlSubmit')
            url =  @$el.find('input[name="webpage-activity-url"]').val()
            if _.trim(url) == ''
                    return false
            if url.indexOf('//') == -1
                    url = 'http://' + url
            unless @$el.find('iframe').length > 0
                    iframe = $('<iframe />').insertAfter(@$el.find('.instructor-ui-box').first())
            @setIframeSrc url
            @$el.find('input[name="webpage-activity-url"]').val('')
Run Code Online (Sandbox Code Playgroud)

这里是编译为javascript的相同函数,以防你不熟悉coffeescript:

WebpageActivityLayout.prototype.onURLSubmit = function(e) {
  var iframe, url;
  e.preventDefault();
  url = this.$el.find('input[name="webpage-activity-url"]').val();
  if (_.trim(url) === '') {
    return false;
  }
  if (url.indexOf('//') === …
Run Code Online (Sandbox Code Playgroud)

html javascript forms jquery coffeescript

0
推荐指数
1
解决办法
1118
查看次数

在方法中的函数中引用CoffeeScript类中的"this"的最佳方法是什么?

希望这个问题有道理,最好的解释方法就是向你展示一个例子.我在CoffeeScript中创建了一个类,并且经常使用@在JS中表示"this",但是,在下面的示例中使用它的最佳方法是什么?目前我将@存储在该方法的局部变量中,我想知道是否有更好的方法

class app

constructor: ->
    # store @ or "this" in a new local var
    $this = @

    # do some stuff

    # Do some sort of jQuery stuff
    $(document).jquerystuff(->
        # do something that references this
        $this.anotherMethod()
    );
Run Code Online (Sandbox Code Playgroud)

javascript jquery coffeescript

0
推荐指数
1
解决办法
141
查看次数

如何用CoffeeScript写$(this)?

我的CoffeeScript代码:

a = (argu='.a') =>
    $(argu).on 'click', () =>
        $(this)
Run Code Online (Sandbox Code Playgroud)

编译为Javascript:

var a,
    _this = this;

a = function(argu) {
    if (argu == null) {
        argu = '.a';
    }
    return $(argu).on('click', function() {
        return $(_this);
    });
};
Run Code Online (Sandbox Code Playgroud)

我希望那this是$(争论)或$('.a')而不是_this.

如何写'这个'可以参考$(争论)?

javascript this coffeescript

0
推荐指数
1
解决办法
61
查看次数

如何在coffeescript中干?

我有以下脚本:

$(".a").click ->
    $("body").animatescroll({scrollSpeed:1500,easing:'easeInOutSine'})

$(".b").click ->
    $("#b").animatescroll({scrollSpeed:1500,easing:'easeInOutSine'})

$(".c").click ->
    $("#c").animatescroll({scrollSpeed:1500,easing:'easeInOutSine'})

$(".d").click ->
    $("#d").animatescroll({scrollSpeed:1500,easing:'easeInOutSine'})
Run Code Online (Sandbox Code Playgroud)

我想这样做:

scroll = animatescroll({scrollSpeed:1500,easing:'easeInOutSine'})

$(".a").click ->
    $("body").scroll

$(".b").click ->
    $("#b").scroll

$(".c").click ->
    $("#c").scroll

$(".d").click ->
    $("#d").scroll
Run Code Online (Sandbox Code Playgroud)

但它返回以下错误: animatescroll is not defined

jquery dry coffeescript

0
推荐指数
1
解决办法
148
查看次数