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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:30:39+00:00 2026-06-17T09:30:39+00:00

I’m improving my knowledge about Backbone.js and have this code sample taken from a

  • 0

I’m improving my knowledge about Backbone.js and have this code sample taken from a tutorial. (http://bardevblog.wordpress.com/2012/01/16/understanding-backbone-js-simple-example/)

This example will not access the server for now, so to simulate the retrieval of data from the server I have a file name movies.json.

What I am trying to do:

  • Add json data in local storage (using localStorage adapter)
  • For this I am using Backbone.ajaxSync, Which Is Given to the alias Backbone.sync by the localStorage adapter: I created the method refreshFromServer () to do this
  • The reason for doing this is that I’m trying to implement a way to get data only one time (and only refresh when i need to)

My issues:
  I’m having an error “Uncaught Error: ‘url’ property or function must be specified” when I call refreshFromServer ().
I do not understand why because I set the url collection. (url : “scripts/data/movies.json” )

Sample code

var Theater = {
    Models : {},
    Collections : {},
    Views : {},
    Templates : {}
}

Theater.Models.Movie = Backbone.Model.extend({})
Theater.Collections.Movies = Backbone.Collection.extend({
    model : Theater.Models.Movie,
    localStorage : new Backbone.LocalStorage("MovieStore"), // Unique name within your app.
    url : "scripts/data/movies.json",
    refreshFromServer : function() {
        return Backbone.ajaxSync.apply(this, arguments);
    },
    initialize : function() {
        console.log("Movies initialize")
    }
});

Theater.Templates.movies = _.template($("#tmplt-Movies").html())

Theater.Views.Movies = Backbone.View.extend({
    el : $("#mainContainer"),
    template : Theater.Templates.movies,

    initialize : function() {

        this.collection.bind("reset", this.render, this);
    },

    render : function() {
        console.log("render")
        console.log(this.collection.length);

    }
})

Theater.Router = Backbone.Router.extend({
    routes : {
        "" : "defaultRoute"
    },

    defaultRoute : function() {
        console.log("defaultRoute");
        Theater.movies = new Theater.Collections.Movies()
        new Theater.Views.Movies({
            collection : Theater.movies
        });
        Theater.movies.refreshFromServer();
        //Theater.movies.fetch();
        console.log(Theater.movies.length)
    }
})

var appRouter = new Theater.Router();
Backbone.history.start();

Notes:

  • If a comment localStorage property in the collection

    Theater.Models.Movie = Backbone.Model.extend({})
    Theater.Collections.Movies = Backbone.Collection.extend({
    model : Theater.Models.Movie,
    //localStorage : new Backbone.LocalStorage(“MovieStore”)
    …
    });

    and then in router call normal fetch method

    Theater.Router = Backbone.Router.extend({
    routes : {
    “” : “defaultRoute”
    },

    defaultRoute : function() {
    
        Theater.movies = new Theater.Collections.Movies()
        new Theater.Views.Movies({
            collection : Theater.movies
        });
        //Theater.movies.refreshFromServer();
        Theater.movies.fetch();
    }
    

    })

I can see the json list correctly in my view

  • If I use the localStorage property in the collection and then call the standard fetch () method, I see only an empty list (I think it is normal as it is read from the local storage and is empty)

  • The error only occurs when using the method refreshFromServer () witch use Backbone.ajaxSync (alias for backbone.sync)

  • 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-17T09:30:40+00:00Added an answer on June 17, 2026 at 9:30 am

    Err… my bad. The refreshFromServer implementation is from my answer to your earlier question., and it’s completely, uselessly wrong.

    Backbone.sync expects arguments (method, model, options), but as it stands, it doesn’t get what it needs from refreshFromServer because the refresh method simply sends forward whatever arguments it gets. Sorry for the mistake.

    The correct, working implementation would be:

    refreshFromServer : function(options) {
        return Backbone.ajaxSync('read', this, options);
    }
    

    It can be used either via success / error callbacks passed to the options hash:

    this.collection.refreshFromServer({ success: function() { /* refreshed... */ });
    

    Or via the jqXHR Promise API:

    this.collection.refreshFromServer().done(function() { /* refreshed... */ })
    

    Or not signing up for callbacks and waiting for the collection reset event like in your example:

    this.collection.bind("reset", this.render, this);
    this.collection.refreshFromServer();
    

    This should work. Please let me know if it doesn’t. I fixed my answer in the previous question too, in case someone stumbles onto it.

    Edit: To save the data to local storage after refreshing you need to manually save each of the models:

    var collection = this.collection;
    collection.refreshFromServer({success: function(freshData) {
        collection.reset(freshData);
        collection.each(function(model) {
            model.save();
        });
    }});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from

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.