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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:54:43+00:00 2026-06-05T14:54:43+00:00

I’ve been trying to learn Backbone. I set up collection of models, each of

  • 0

I’ve been trying to learn Backbone.

I set up collection of models, each of which has it’s own view that should render every time the model syncs with the server. The server sends back an ID for the model. It’s seems to be rendering before the model ID is set. If you look at the console.log() output, I have it print the model’s id just before it renders. I also have it print each event as it happens. I get:

undefined (model id)
add
change:id
change (not sure what changes here?)
sync

Also, I have it change the model’s name attribute after a 2 sec pause model.save('name', 'New Name'). This causes another sync event, but the view doesn’t update.

Here’s the working code: http://jsfiddle.net/flackend/brB7y/1/

Or see it here:

var game_library;

(function($) {

    _.templateSettings = {
        interpolate: /\{(.+?)\}/g
    };    

    var GameModel = Backbone.Model.extend({
        url: '/gh/gist/response.json/2895708/',
        name: undefined,
        initialize: function() {

            // Create a view for this model
            this.view = new GameView({model: this});

            // Sync model with server
            this.save();

            // Bind events
            this.on('sync', this.view.render());
            this.on('all', this.console);
        },
        console: function(event, model, changes) {

            console.log('['+ ++this.i +']-------------(event[:attr], model, changes)----------------');
            console.log(event);
            console.log(model);
            console.log(changes);
        },
        i: 0
    });

    var GameCollection = Backbone.Collection.extend({
        model: GameModel
    });

    var GameView = Backbone.View.extend({
        el: $('#holder'),
        template: $('#game-template').html(),
        render: function() {

            var template = _.template(this.template);
            console.log(this.model.id);
            this.$el.html(template(this.model.toJSON()));
        }
    });

    // Instantiate new game collection
    game_library = new GameCollection;

    // Add a game
    // Note: can only pass in 1 ID from gist, so only add 1 game.
    var games = [
        new GameModel({name: 'Skyrim'})
    ];

    // Note: `game_library.add(new GameModel({name: 'Skyrim'}));` does
    // not work for some reason having to do with instances...
    game_library.add(games);

    // 2 sec later...
    window.setTimeout(function() {

        game_library.get(1).save('name', 'The Elder Scrolls V: Skyrim');
    }, 2000);
})( jQuery );

Thanks for any help!!​

  • 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-05T14:54:46+00:00Added an answer on June 5, 2026 at 2:54 pm

    Updated gist at http://jsfiddle.net/brB7y/2/. I’ve moved your refresh call to a local function which works. I’m not sure about the technical reasons behind this but it’s how I’ve used it before.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small

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.