使用jquery将基于coffeescript类的数据结构上传到ruby on Rails 3?

发布于 2025-01-03 06:27:08 字数 987 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-01-10 06:27:08

您可以做的一件事可能会在将来对您有所帮助,那就是添加 toJSON 方法:

如果正在字符串化的对象具有名为 toJSON 的属性,其值为函数,则 toJSON 方法会自定义 JSON 字符串化行为:而不是正在序列化的对象, toJSON 方法在调用时返回的值将被序列化。

这或多或少类似于 Rails 中的 to_json。不幸的是,jQuery 不会自动调用它,但 JSON 库会自动调用它,Backbone 也会如此(如果您最终使用 Backbone);添加 toJSON 方法还允许对象控制其自身的序列化。

然后向您的 $.ajax 添加一个 toJSON 调用:

$.ajax(
  type: "POST"
  url: '/crosses/save'
  data: cross_data.toJSON()
  contentType: 'json'
  success: (msg) -> 
    alert( "Data Saved: " + msg )
)

或者,使用最近的 jQuery,您可以设置一个 AJAX 预过滤器 在可用时自动调用 toJSON

$.ajaxPrefilter( (opts, original_opts) ->
    if(original_opts.data && original_opts.data.toJSON && $.isFunction(original_opts.data.toJSON)
        opts.data = $.param(original_opts.data.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:

If an object being stringified has a property named toJSON whose value is a function, then the toJSON method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON method when called will be serialized.

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 a toJSON method also allows the object to control its own serialization.

Then add a toJSON call to your $.ajax:

$.ajax(
  type: "POST"
  url: '/crosses/save'
  data: cross_data.toJSON()
  contentType: 'json'
  success: (msg) -> 
    alert( "Data Saved: " + msg )
)

Or, with recent jQuery's, you could set up an AJAX pre-filter to automatically call toJSON when it is available:

$.ajaxPrefilter( (opts, original_opts) ->
    if(original_opts.data && original_opts.data.toJSON && $.isFunction(original_opts.data.toJSON)
        opts.data = $.param(original_opts.data.toJSON())
)

Then if you called $.ajax with a data that had a toJSON method, toJSON would get called to serialize the data object.

You could also set up a simple recursive $.ajaxPrefilter to automatically skip over anything in original_opts.data that $.isFunction detected as a function. You'd want to use $.isPlainObject to skip over data that is already a string though.

I don't know of any plugins that do this automatically though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文