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 9270609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:24:48+00:00 2026-06-18T15:24:48+00:00

Here is a demo , how to load a model with associations (in a

  • 0

Here is a demo, how to load a model with associations (in a single request) in canJS.
I found it in the github repo of canJS, here, and actually I had to rewrite it a little bit to work (it was outdated), but it works now.

My problem is, that if I change a loaded contact (contact.attr('name', 'Tom');), and then I want to save it (contact.save();), then the contact, and also the contact’s tasks will be posted via ajax to the server, to be saved. This is logical, because the tasks is an attribute of contact.

My problem is, that I only want the contact name, birthday and id to be posted when updating a record. I probably should override the makeRequest method, and remove tasks before posting to server, but I thinks there should be a more elegant solution.

Hopefully there are some canJS users here, who handled this situation already.

  • 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-06-18T15:24:50+00:00Added an answer on June 18, 2026 at 3:24 pm

    I was searching for a solution to this very problem myself, and I came up with my own one, inspired by trickey’s suggestion on the JavascriptMVC forum in this post about modifying the serialize() model method to make it play more nicely with Rails.

    Like his suggestion, I created my own extension of the Model class, and I added an include field on my models where you can list the attributes you’d like to include (patterned after the Rails as_json method).

    can.Model("BaseModel", {}, {
      serialize: function() {
        var data, retval, serialized;
        data = {};
        retval = {};
        serialized = can.Model.prototype.serialize.call(this);
        //check if we're using the "include" fields or not
        if (typeof this.constructor.include !== 'undefined') {
          can.each(this.constructor.include, function(attr) {
            data[attr] = serialized[attr];
          });
        } else {
          data = serialized;
        }
        //wrap the return value in the model name for Rails purposes, e.g. {"event": {data}}
        retval[this.constructor._shortName] = data;
        return retval;
      }
    });
    

    Then in my model I use the “include” array to indicate which fields I want to include. By omitting “attendees” and “speakers”, those associated models will not be packaged into my serialized JSON that gets sent back to the server.

    Event = BaseModel.extend('Event', {
      findAll: "GET /admin/events",
      findOne: "GET /admin/events/{id}",
      create: "POST /admin/events",
      update: "PUT /admin/events/{id}",
      destroy: "DELETE /admin/events/{id}",
      attributes: {
        attendees: 'Models.User.models',
        speakers: 'Models.User.models'
      },
      include: ['id', 'name', 'start_time', 'end_time']
    }, {});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to implement the endless pagination jquery plugin found here ( https://github.com/jney/jquery.pageless )
Here's my demo (permanent link; might take a second to load). I'm having a
I found this site/tutorial/demo from another question here on SO. Very nice and clean
Please see demo here: http://jsfiddle.net/mA6qm/1/ (expand console) Why aren't events being sent or received
Please see the demo here: http://jsfiddle.net/Freewind/Vkp4U/ The html: <div class=outer> <div class=x>x</div> <div class=y>y</div>
Consider the following example: ( live demo here ) HTML: <div class=board> <div class=row>
Im having a big headcache with a header design: Here the demo: http://jsfiddle.net/uGECm/ The
I'm trying to build a simple grid of div s: ( live demo here
Here is the DEMO HTML CODE as <div id=me></div> <a id=ok href=>OK</a> jquery code:
Here is what I want to do: class demo(object): def a(self): pass def b(self,

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.