小编tom*_*ian的帖子

如何使用Coffeescript的params编写setTimeout

请告诉我如何在coffeescript中写下面的javascript.

setTimeout(function(){
    something(param);
}, 1000);
Run Code Online (Sandbox Code Playgroud)

javascript settimeout coffeescript

206
推荐指数
5
解决办法
7万
查看次数

如何在Backbone.Controller中检测无效路由和触发器功能

是否有任何方法可以在Backbone.Controller中检测无效(或未定义)路由并触发404页面?

我已经在我的控制器中定义了这样的路由,但它没有用.

class MyController extends Backbone.Controller
    routes:
        "method_a": "methodA"
        "method_b": "methodB"
        "*undefined": "show404Error"

    # when access to /#method_a
    methodA: ->
        console.log "this page exists"

    # when access to /#method_b
    methodB: ->
        console.log "this also exists"

    # when access to /#some_invalid_hash_fragment_for_malicious_attack
    show404Error: ->
        console.log "sorry, this page does not exist"
Run Code Online (Sandbox Code Playgroud)

更新:

我使用Backbone.Controller的构造函数来匹配当前的哈希片段和@routes.

class MyController extends Backbone.Controller
    constructor: ->
        super()
        hash = window.location.hash.replace '#', ''
        if hash
            for k, v of @routes
                if k is hash
                    return
                @show404Error()

    routes:
        "method_a": "methodA"
        "method_b": …
Run Code Online (Sandbox Code Playgroud)

coffeescript backbone.js

8
推荐指数
1
解决办法
8201
查看次数

如何在自动缩放时同步我的EC2实例

在为应用程序自动扩展我的EC2实例时,保持每个实例同步的最佳方法是什么?

例如,有下面的自定义设置和应用程序文件...

  • Apache httpd.conf
  • php.ini中
  • 我的应用程序的PHP源代码

为了使我的自动缩放工作,所有这些必须在每个EC2实例中配置相同,并且我想知道同步这些元素的最佳实践.

replication amazon-ec2

7
推荐指数
1
解决办法
3942
查看次数

如何在编组类型时将方法结果嵌入到JSON输出中?

我正在寻求一种干净的方法,将方法的返回值嵌入到类型/值的JSON编组中.如果我不需要编写自定义JSON marshaller,那就太好了.

例如,如果用户类型具有FirstNameLastName字段和FullName()方法,我怎样才能容易地嵌入一个full_name字段到JSON输出?

 type User struct {
     FirstName string `json:"first_name"`
     LastName  string `json:"last_name"`
 }

 func (u User) FullName() string {
     return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
 }
Run Code Online (Sandbox Code Playgroud)

预期的JSON:

 {
     "first_name": "John",
     "last_name":  "Smith",
     "full_name":  "John Smith"
 }
Run Code Online (Sandbox Code Playgroud)

json struct marshalling go

3
推荐指数
1
解决办法
127
查看次数

Golang,按字母顺序对结构字段进行排序

我如何得到struct的输出,按字段排序?

type T struct {
    B int
    A int
}

t := &T{B: 2, A: 1}

doSomething(t)

fmt.Println(t)  // &{1 2} --> Sorted by fields
Run Code Online (Sandbox Code Playgroud)

sorting struct go

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