使用jquery将基于coffeescript类的数据结构上传到ruby on Rails 3?
我有一个 coffescript 数据结构,它由一个包含其他对象数组的对象组成,这些对象具有辅助方法,可以更轻松地操作数据结构中的数据:
class CrossData
species_list: {
species1: [21,10,30,40]
}
constructor: () ->
@species = "defualt"
@donors = [new CrossDonor] #array that will be full of CrossDonors
helper_method: (arg1) =>
#do stuff with crossdata
class CrossDonor
constructor: () ->
@name = "default"
@linkage_group = -1
@trait_cm = -1
helper_method: (arg1) ->
#do stuff with cross donor
cross_data = new CrossData
如果我尝试通过 jquery ajax 上传它,即:
$.ajaxSetup(
beforeSend: (xhr) ->
xhr.setRequestHeader('X-CSRF-Token',
$('meta[name="csrf-token"]').attr('content')))
$.ajax(
type: "POST",
url: '/crosses/save',
data: cross_data
contentType: 'json'
success: (msg) ->
alert( "Data Saved: " + msg )
)
它不会因为它会在函数上出错,所以我想答案是创建一个函数来生成咖啡脚本类的 json 版本。我想知道是否有一个 jquery 插件可以给我一个 Coffee_class 数据类型?
I have a coffescript datastructure which consists of an object which has arrays of other objects, these objects have helper methods which make it easier to manipulate the data in the data structure:
class CrossData
species_list: {
species1: [21,10,30,40]
}
constructor: () ->
@species = "defualt"
@donors = [new CrossDonor] #array that will be full of CrossDonors
helper_method: (arg1) =>
#do stuff with crossdata
class CrossDonor
constructor: () ->
@name = "default"
@linkage_group = -1
@trait_cm = -1
helper_method: (arg1) ->
#do stuff with cross donor
cross_data = new CrossData
if I try and upload this via jquery ajax ie:
$.ajaxSetup(
beforeSend: (xhr) ->
xhr.setRequestHeader('X-CSRF-Token',
$('meta[name="csrf-token"]').attr('content')))
$.ajax(
type: "POST",
url: '/crosses/save',
data: cross_data
contentType: 'json'
success: (msg) ->
alert( "Data Saved: " + msg )
)
it doesn't work since it trips up over the functions, I guess the answer is to create a function which produces a json verison of the coffee script classes. I'm wondering if there is a jquery plugin which will give me a coffee_class data type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的一件事可能会在将来对您有所帮助,那就是添加
toJSON
方法:这或多或少类似于 Rails 中的
to_json
。不幸的是,jQuery 不会自动调用它,但 JSON 库会自动调用它,Backbone 也会如此(如果您最终使用 Backbone);添加 toJSON 方法还允许对象控制其自身的序列化。然后向您的
$.ajax
添加一个toJSON
调用:或者,使用最近的 jQuery,您可以设置一个 AJAX 预过滤器 在可用时自动调用
toJSON
:然后,如果您调用
$.ajax 与
data
具有toJSON
方法,将调用toJSON
来序列化data
对象。您还可以设置一个简单的递归
$.ajaxPrefilter
来自动跳过original_opts.data
中$.isFunction
检测为函数。您希望使用$.isPlainObject
跳过 < code>data 虽然已经是一个字符串了。但我不知道有任何插件可以自动执行此操作。
One thing you could do that might help you in the future would be to add a
toJSON
method:This is more or less like
to_json
in Rails. jQuery won't call it automatically, unfortunately, but the JSON library will and so will Backbone (if you end up using Backbone); adding atoJSON
method also allows the object to control its own serialization.Then add a
toJSON
call to your$.ajax
:Or, with recent jQuery's, you could set up an AJAX pre-filter to automatically call
toJSON
when it is available:Then if you called
$.ajax
with adata
that had atoJSON
method,toJSON
would get called to serialize thedata
object.You could also set up a simple recursive
$.ajaxPrefilter
to automatically skip over anything inoriginal_opts.data
that$.isFunction
detected as a function. You'd want to use$.isPlainObject
to skip overdata
that is already a string though.I don't know of any plugins that do this automatically though.