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

  • SEARCH
  • Home
  • 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 6988467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:05:07+00:00 2026-05-27T19:05:07+00:00

Code: class Session extends Backbone.Model initialize: -> @bind ‘change’, @save console.log ‘init’ class SessionList

  • 0

Code:

  class Session extends Backbone.Model
    initialize: ->
      @bind 'change', @save
      console.log 'init'
  class SessionList extends Backbone.Collection
    model: Session
    localStorage: new Store 'sessions'

  sessions = new SessionList
  a = new Session x: 'test'
  sessions.add a    
  console.log a.get 'x'
  a.set x: 'new'
  console.log a.get 'x'

When loaded in a page with Backbone.localstorage, the console gives:

init
test
Uncaught TypeError: Cannot read property 'localStorage' of undefined
  backbone-localstorage.js:70
Backbone.sync
_.extend.save
  backbone-localstorage.js:70
Backbone.Events.trigger
  backbone.js:304
_.extend.change
  backbone.js:117

And when I comment out the @bind call, I get the expected:

init
test
new

I can also save manually successfully after a has been added to sessions with a call to a.save().

I guess the problem is that the Session constructor triggers the change event, and save() doesn’t know what to do before a has been added to sessions? So I could instead do something like this:

class Session extends Backbone.Model
  set: (fields, ops) ->
    super fields, ops
    if (this has been added to a Collection)
      @save() 

Is this the best way to do it? If yes, how do I fill in the if statement?

  • 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-27T19:05:08+00:00Added an answer on May 27, 2026 at 7:05 pm

    My suggestion would be to just call save instead of set. so replace this:

    a.set x: 'new'

    with

    a.save x: 'new'

    hope that works for you

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

Sidebar

Related Questions

Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True,
The following code: class Log: BAT_STATS = ['AB', 'R', 'H', 'HR'] def __init__(self, type):
CakePHP is driving me nuts! Here is my code: if($this->Page->save($datavalue)) { $this->Session->setFlash('Page content updated
My HotelAddress Model: <?php class HotelAddress extends AppModel { var $name = 'HotelAddress'; var
If I have Python code class A(): pass class B(): pass class C(A, B):
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
Why does this code: class A { public: explicit A(int x) {} }; class
When I try this code: class MyStuff: def average(a, b, c): # Get the
Consider the following code: class Program { static void Main(string[] args) { Department deathStar

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.