0

我正在尝试使用 Backbone 1.1.2 和 Rails 4.2.beta1 创建一个用户帐户。

在骨干中,我打电话给:

public create(){
    var email:string = $('#create-account-email').val(), password:string = $('#create-account-password').val(), passconf:string = $('#create-account-password-confirm').val();
    this.model.set({email: email, password: password, password_confirmation: passconf});
    this.model.save(null, {success: this.success, error: this.error});
}

create它使用以下请求参数正确调用了我的 web 服务中的 rails方法:

{"email":"test@test.com","password":"123456","password_confirmation":"123456"}

它正在通过过滤器方法

def account_params
  #breakpoint
  params.require(:account).permit(:password, :password_confirmation, :email)
end

但是如果我在上述位置放置一个断点并检查params我得到的对象:

{"email"=>"test@test.com", 
 "password"=>"123456", 
 "password_confirmation"=>"123456", 
 "controller"=>"accounts", 
 "action"=>"create", 
 "account"=>{"email"=>"test@test.com"}, 
 "format"=>"json"}

乍一看,这对我来说是正确的。但是创建帐户失败,因为密码是nil

如果检查结果account_params我只得到:

{"email" => "test@test.com")

为什么密码不包含在帐户参数对象中。我正在使用所有带有rails的默认支架代码和Backbone的默认配置。

4

1 回答 1

0

我为 ruby​​ gem主干轨找到了一个 GitHub 存储库,并使用了他们在此处包含的同步覆盖。

Backbone._sync = Backbone.sync;
Backbone.sync = function (method:string, model:Backbone.Model, options?: any){
    if(options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
        options.contentType = 'application/json';
        var data:any = JSON.stringify(options.attrs || model.toJSON(options));
        if(model.paramRoot) {
            data = {};
            data[model.paramRoot] = model.toJSON(options);
        } else {
            data = model.toJSON();
        }
        options.data = JSON.stringify(data);
    }
    return Backbone._sync(method, model, options);
}

我删除了一些我不需要的项目,但这会将主干属性包装到一个对象中,并以它需要的格式将其发送到 rails 应用程序。

所以参数:

{"email":"test@test.com",
 "password":"123456",
 "password_confirmation":"123456"}

变得:

{"account":
    {"email":"test@test.com",
     "password":"123456",
     "password_confirmation":"123456"}
}

这并非没有对主干应用程序代码进行一点修改......

当我使用打字稿时,我必须使用以下签名将_sync()方法添加到文件中。backbone.d.ts

declare module Backbone {

    function _sync(method: string, model: Model, options?: JQueryAjaxSettings):any;

    ...
}

我还需要添加以下内容Backbone.Model

class Model extends ModelBase implements OptionalDefaults {

    paramRoot: string;

    ...
}

然后我将该paramRoot属性添加到我的模型中:

export class Account extends Backbone.Model {
    public urlRoot: string;
    public validation:any;
    public paramRoot:string;
    constructor(attributes?: any, options?: any){
        this.paramRoot = 'account';
        this.urlRoot = 'http://mydomain.fake/accounts';
        this.validation = {
            email: {
                required: true,
                pattern: 'email'
            },
            password: {
                required: true,
                minLength: 6
            }
        };
        super(attributes, options);
    }
}

还有其他方法可以让_sync()方法和paramRoot属性在不修改文件的情况下传递编译器错误,backbone.d.ts而是扩展对象并将其添加到另一个.d.ts文件中。但这对我来说没问题,因为我计划在未来的项目中使用它,我不介意.d.ts稍微修改文件。

希望这对将来的某人有所帮助。

于 2014-10-06T01:00:58.100 回答