Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:39:18+00:00 2026-05-29T12:39:18+00:00

I have a coffescript datastructure which consists of an object which has arrays of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:39:19+00:00Added an answer on May 29, 2026 at 12:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really simple coffescript class: class Bar foo: -> console.log('bar') Which when
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a regex problem which bugs me and have no clue how to
I have a .coffeescript.erb file in which I would like to get CSRF meta-tag
I have seen coffescript tutorials that show how to use coffeescript with rails, nodejs,
I'm trying to write some CoffeScript function which checks all checkboxes in a table
What's the best way to chain methods in CoffeeScript? For example, if I have
I have a CoffeeScript which I can't call functions from. But if I declare
If I have a CoffeeScript class defined in a separate file, which I'm calling
I have a view helper, let say (for simplicity's sake) def call_alert return alert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.