Backbone collection's URL depends on initialize function

Posted by egidra on Stack Overflow See other posts from Stack Overflow or by egidra
Published on 2012-04-03T23:19:40Z Indexed on 2012/04/03 23:29 UTC
Read the original article Hit count: 205

Filed under:
|

I have a Backbone collection whose URL depends on the initialize function. When I create an instance of this Backbone collection, I pass in an ID to filter which instances of the model appear. Here is what the collection's code looks like:

  var GoalUpdateList = Backbone.Collection.extend({

    // Reference the Goal Update model
    model: GoalUpdate,

    // Do HTTP requests on this endpoint
    url: "http://localhost:8000/api/v1/goal_update/?goal__id=" + this.goal_id + "&format=json",

    // Set the goal ID that the goal update list corresponds to
    initialize: function(goal_id) {
      this.goal_id = goal_id;
      console.log(this.goal_id);
      console.log(this.url);
    },

  });

Of course, this doesn't work. this.goal_id is seen as being undefined. I guess because the URL is set before the initialization function is run.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about backbone.js